두 수, a b를 입력받은 후 더해서 출력합니다.
타입을 u8로 사용한 것에 특별한 의미는 없습니다.
1
2
3
4
5
6
7
8
9
10
11
12 | use std::io::{self, Read};
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
let mut it = input.split_whitespace();
let a: u8 = it.next().unwrap().parse().unwrap();
let b: u8 = it.next().unwrap().parse().unwrap();
println!("{}", a + b);
}
|
두 수, a b를 입력받은 후 빼서 출력합니다.
방금 전 타입으로는 음수를 표현하지 못하니 타입을 수정하고 뺄셈으로 바꾸어 줍니다.
1
2
3
4
5
6
7
8
9
10
11
12 | use std::io::{self, Read};
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
let mut it = input.split_whitespace();
let a: i8 = it.next().unwrap().parse().unwrap();
let b: i8 = it.next().unwrap().parse().unwrap();
println!("{}", a - b);
}
|
두 수, a b를 입력받은 후 곱해서 출력합니다.
그럴 이유는 없지만 다시 타입을 unsigned로 바꾸었습니다 ㅋㅋ!
1
2
3
4
5
6
7
8
9
10
11
12 | use std::io::{self, Read};
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
let mut it = input.split_whitespace();
let a: u8 = it.next().unwrap().parse().unwrap();
let b: u8 = it.next().unwrap().parse().unwrap();
println!("{}", a * b);
}
|
두 수, a b를 입력받은 후 더하고, 빼고, 곱하고, 몫을 구하고, 나머지를 구하여 출력합니다.
최대 결과가 1억이니 타입을 잘못 선택해 문제가 발생하는 일이 없도록 합시다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | use std::io::{self, Read};
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
let mut it = input.split_whitespace();
let a: i32 = it.next().unwrap().parse().unwrap();
let b: i32 = it.next().unwrap().parse().unwrap();
println!("{}", a + b);
println!("{}", a - b);
println!("{}", a * b);
println!("{}", a / b);
println!("{}", a % b);
}
|

타입 실수로 WA를 받은 모습
a, b를 나누어 출력합니다.
타입을 잘못 선택해 오차가 크게 발생하는 일이 없도록 해야 합니다.
새싹 문제에서는 처음 만나는 상대/절대 오차 문제입니다.
1
2
3
4
5
6
7
8
9
10
11
12 | use std::io::{self, Read};
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
let mut it = input.split_whitespace();
let a: f64 = it.next().unwrap().parse().unwrap();
let b: f64 = it.next().unwrap().parse().unwrap();
println!("{}", a / b);
}
|
마지막 입력과 계산 새싹 문제입니다.
1
2
3
4
5
6
7
8
9
10
11
12
13 | use std::io::{self, Read};
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
let mut it = input.split_whitespace();
let a: i64 = it.next().unwrap().parse().unwrap();
let b: i64 = it.next().unwrap().parse().unwrap();
let c: i64 = it.next().unwrap().parse().unwrap();
println!("{}", a + b + c);
}
|