이상한 기호

왜 함수 카테고리일까요?
타입 괜히 작게 잡아서 터지면 조금 속상할 수 있으니 주의해야 합니다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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();
    println!("{}", (a - b) * (a + b));
}

함수를 (쓸 이유는 없지만) 써봅시다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
use std::io::{self, Read};

fn at(a: i64, b: i64) -> i64 {
    (a + b) * (a - b)
}

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();
    println!("{}", at(a, b));
}

검증수

이번에도 함수로 만들 필요는 없었지만 함수로 해결했습니다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
use std::io::{self, Read};

fn create_checksum(a: u8, b: u8, c: u8, d: u8, e: u8) -> u8 {
    (a * a + b * b + c * c + d * d + e * e) % 10
}

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();
    let c: u8 = it.next().unwrap().parse().unwrap();
    let d: u8 = it.next().unwrap().parse().unwrap();
    let e: u8 = it.next().unwrap().parse().unwrap();

    println!("{}", create_checksum(a, b, c, d, e));
}

마지막 새싹 문제였습니다.
마지막 코드는 조금 보기 힘들지 않은가요?
언젠가 입력 구조체를 만들고 글을 쓸 수 있으면 좋겠네요.

🎉 새싹 문제 클리어!