일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 자바
- 파이썬
- 데이터 통신
- 파이썬 알고리즘
- 백준 러스트
- 데이터베이스
- 오라클
- Python challenge
- 우분투
- ubuntu
- 백준
- data communication
- Operating System
- 자바 기초
- 러스트 프로그래밍 공식 가이드
- 러스트 예제
- Reversing
- java
- Rust
- 자바 개념
- 파이썬 첼린지
- 알고리즘
- 파이썬 챌린지
- 운영체제
- 러스트
- 오라클DB
- C
- OS
- Database
- Python
Archives
- Today
- Total
IT’s Portfolio
[Algorithm] Baekjoon - 입출력과 사칙연산 단계 본문
728x90
반응형
💻 Baekjoon I/O & Calculation Stage
Hello World
fn main() {
println!("Hello World!");
}
A+B
Basic Code
use std::io;
fn main() {
let mut numbers = String::new();
let split_nums: Vec<&str> = match io::stdin().read_line(&mut numbers) {
Ok(_n) => numbers.split(' ').collect(),
Err(_) => vec!["Error"],
};
let a: u32 = match split_nums[0].trim().parse() {
Ok(x) => x,
Err(_) => 0,
};
let b: u32 = match split_nums[1].trim().parse() {
Ok(x) => x,
Err(_) => 0,
};
println!("{}", a+b);
}
Improvement Code
use std::io::*;
fn main() {
let mut numbers = [0; 3];
stdin().read(&mut numbers)
.expect("No input");
println!("{}", numbers[0]+numbers[2]-96);
}
struct_Stdin in Official Document
function_stdin in Official Document
trait_Read in Official Document
A-B
Basic Code
use std::io;
fn main() {
let mut numbers = String::new();
let split_nums: Vec<&str> = match io::stdin().read_line(&mut numbers) {
Ok(_n) => numbers.split(' ').collect(),
Err(_) => vec!["Error"],
};
let a: i32 = match split_nums[0].trim().parse() {
Ok(x) => x,
Err(_) => 0,
};
let b: i32 = match split_nums[1].trim().parse() {
Ok(x) => x,
Err(_) => 0,
};
println!("{}", a-b);
}
Improvement Code
use std::io;
fn main() {
let mut numbers = String::new();
io::stdin().read_line(&mut numbers)
.expect("No input");
let split_nums: Vec<i8> = numbers.split(' ')
.map(
|x| x.trim().parse().expect("Error")
).collect();
println!("{}", split_nums[0]-split_nums[1]);
}
struct_Map in Official Document
trait_Iterator_method_collect in Official Document
A$\times$B
Basic Code
use std::io;
fn main() {
let mut numbers = String::new();
let split_nums: Vec<&str> = match io::stdin().read_line(&mut numbers) {
Ok(_n) => numbers.split(' ').collect(),
Err(_) => vec!["Error"],
};
let a: u32 = match split_nums[0].trim().parse() {
Ok(x) => x,
Err(_) => 0,
};
let b: u32 = match split_nums[1].trim().parse() {
Ok(x) => x,
Err(_) => 0,
};
println!("{}", a*b);
}
Improvement Code
use std::io;
fn main() {
let mut numbers = String::new();
io::stdin().read_line(&mut numbers)
.expect("No input");
println!(
"{:?}",
numbers.split(' ')
.map(
|x| x.trim().parse().expect("error")
).fold(1, |a, b: i32| a*b)
);
}
trait_Iterator_method_fold in Official Document
A/B
Basic Code
use std::io;
fn main() {
let mut numbers = String::new();
let split_nums: Vec<&str> = match io::stdin().read_line(&mut numbers) {
Ok(_n) => numbers.split(' ').collect(),
Err(_) => vec!["Error"],
};
let a: f64 = match split_nums[0].trim().parse() {
Ok(x) => x,
Err(_) => 0.0,
};
let b: f64 = match split_nums[1].trim().parse() {
Ok(x) => x,
Err(_) => 0.0,
};
println!("{}", a/b);
}
Improvement Code
use std::io;
fn main() {
let mut numbers = String::new();
io::stdin().read_line(&mut numbers)
.expect("No input");
let split_nums: Vec<f64> = numbers.split(' ')
.map(
|x| x.trim().parse().expect("error")
).collect();
println!("{}", split_nums[0]/split_nums[1]);
}
Four Arithmetic Operations
use std::io;
fn main() {
let mut numbers = String::new();
io::stdin().read_line(&mut numbers)
.expect("No Input");
let a: Vec<i32> = numbers.split(' ')
.map(
|x| x.trim().parse().expect("Error")
).collect();
println!(
"{}\n{}\n{}\n{}\n{}",
a[0]+a[1],
a[0]-a[1],
a[0]*a[1],
a[0]/a[1],
a[0]%a[1]
);
}
??!
Basic Code
use std::io;
fn main() {
let mut id = String::new();
io::stdin().read_line(&mut id)
.expect("Error");
id = id.trim().to_string() + "??!";
println!("{id}");
}
Improvement Code
use std::io;
fn main() {
let mut id = String::new();
io::stdin().read_line(&mut id)
.expect("Error");
println!("{}??!", id.trim());
}
Buddhism Year
- 서기 연도와 불기 연도의 차이는
543
년임
Basic Code
use std::io;
fn main() {
let mut year = String::new();
io::stdin().read_line(&mut year)
.expect("No Input");
let bud_year: u16 = year.trim().parse()
.expect("Error");
println!("{}", bud_year-543);
}
Improvement Code
use std::io;
fn main() {
let mut year = String::new();
io::stdin().read_line(&mut year)
.expect("No Input");
println!(
"{}",
year.trim().parse::<u16>().expect("Error") - 543
);
}
Chess Pieces
Basic Code
use std::io;
const CHESS_SET: [i32; 6] = [1, 1, 2, 2, 2, 8];
fn main() {
let mut a = String::new();
io::stdin().read_line(&mut a)
.expect("Error");
let pieces: Vec<i32> = a.split(' ')
.map(
|x| x.trim().parse().expect("error")
).collect();
for i in 0..6 {
print!("{} ", CHESS_SET[i]-pieces[i]);
}
}
Modulo
Basic Code
use std::io;
fn main() {
let mut a = String::new();
io::stdin().read_line(&mut a)
.expect("Error");
let nums: Vec<u32> = a.split(' ')
.map(
|x| x.trim().parse().expect("error")
).collect();
println!(
"{}\n{}\n{}\n{}",
(nums[0]+nums[1])%nums[2],
((nums[0]%nums[2])+(nums[1]%nums[2]))%nums[2],
(nums[0]*nums[1])%nums[2],
((nums[0]%nums[2])*(nums[1]%nums[2]))%nums[2]
);
}
Additional Code
use std::io;
fn main() {
let mut z = String::new();
io::stdin().read_line(&mut z)
.expect("Error");
match &z.split(' ')
.map(
|x| x.trim().parse().expect("Error")
).collect::<Vec<u32>>()[..3] {
&[a, b, c] => {
println!(
"{}\n{}\n{}\n{}",
(a+b)%c, ((a%c)+(b%c))%c,
(a*b)%c, ((a%c)*(b%c))%c
);
},
_ => (),
}
}
Multiplication
Basic Code
use std::io;
fn main() {
let mut a = String::new();
let mut b = String::new();
io::stdin().read_line(&mut a)
.expect("Error");
io::stdin().read_line(&mut b)
.expect("Error");
let mul_nums: [u32; 2] = [
a.trim().parse().expect("Error"),
b.trim().parse().expect("Error")
];
println!(
"{}\n{}\n{}\n{}",
mul_nums[0]*(mul_nums[1]%10),
mul_nums[0]*((mul_nums[1]%100)/10),
mul_nums[0]*(mul_nums[1]/100),
mul_nums[0]*mul_nums[1]
);
}
Improvement Code
use std::io;
fn toss_u32() -> u32 {
let mut a = String::new();
io::stdin().read_line(&mut a)
.expect("Error");
a.trim().parse().expect("Error")
}
fn main() {
let (n1, n2) = (toss_u32(), toss_u32());
println!(
"{}\n{}\n{}\n{}",
n1*(n2%10),
n1*((n2%100)/10),
n1*(n2/100),
n1*n2
);
}
Cat
Basic Code
fn main() {
println!("\\ /\\\n ) ( ')\n( / )\n \\(__)|");
}
Dog
Basic Code
fn main() {
print!("|\\_/|\n");
print!("|q p| /}}\n");
print!("( 0 )\"\"\"\\\n");
print!("|\"^\"` |\n");
print!("||_/=\\\\__|\n");
}
Improvement Code
fn main(){
print!(r#"|\_/|
|q p| /}}
( 0 )"""\
|"^"` |
||_/=\\__|"#);
}
Sprout
Basic Code
fn main(){
print!(r#" ,r'"7
r`-_ ,' ,/
\. ". L_r'
`~\/
|
|"#);
}
------------------ 2023.03.12 추가 문제 ------------------
JeongMin
Basic Code - 1
use std::io;
fn main() {
let mut number = String::new();
io::stdin().read_line(&mut number).unwrap();
let nums: Vec<u64> = number.trim().split(' ')
.map(
|x| x.parse().unwrap()
).collect();
println!("{}", nums[0]+nums[1]+nums[2]);
}
Basic Code - 2
use std::io;
fn main() {
let mut number = String::new();
let mut tmp = 0;
io::stdin().read_line(&mut number).unwrap();
let nums: Vec<u64> = number.trim().split(' ')
.map(
|x| {
match x.parse::<u64>() {
Ok(x) => {tmp+=x; tmp},
Err(_) => 0,
}
}
).collect();
println!("{}", nums[2]);
}
728x90
반응형
'Development Study > Rust' 카테고리의 다른 글
Comments