아스키 코드

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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 c: char = it.next().unwrap().parse().unwrap();
    println!("{}", c as u8)
}

단어 길이 재기

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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 s: &str = it.next().unwrap();
    println!("{}", s.len());
}

대소문자 바꾸기

 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 main() {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    let mut it = input.split_whitespace();

    let s: &str = it.next().unwrap();

    let converted_s: String = s
        .chars()
        .map(|c| match c {
            c if c.is_ascii_uppercase() => c.to_ascii_lowercase(),
            c if c.is_ascii_lowercase() => c.to_ascii_uppercase(),
            _ => unreachable!(),
        })
        .collect();
    println!("{}", converted_s);
}

음…

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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 s: &str = it.next().unwrap();

    let converted_s: String = s
        .chars()
        .map(|c| {
            if c.is_ascii_uppercase() {
                c.to_ascii_lowercase()
            } else {
                c.to_ascii_uppercase()
            }
        })
        .collect();
    println!("{}", converted_s);
}

학점계산

 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
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 grade = it.next().unwrap();

    println!(
        "{:.1}",
        match grade {
            "A+" => 4.3,
            "A0" => 4.0,
            "A-" => 3.7,
            "B+" => 3.3,
            "B0" => 3.0,
            "B-" => 2.7,
            "C+" => 2.3,
            "C0" => 2.0,
            "C-" => 1.7,
            "D+" => 1.3,
            "D0" => 1.0,
            "D-" => 0.7,
            "F" => 0.0,
            _ => unreachable!(),
        }
    );
}

숫자가 자연스러우니 그렇게 했지만 문자열로 출력해 볼 수도 있겠죠 (바람직하지 않더라도)

 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
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 grade = it.next().unwrap();

    println!(
        "{}",
        match grade {
            "A+" => "4.3",
            "A0" => "4.0",
            "A-" => "3.7",
            "B+" => "3.3",
            "B0" => "3.0",
            "B-" => "2.7",
            "C+" => "2.3",
            "C0" => "2.0",
            "C-" => "1.7",
            "D+" => "1.3",
            "D0" => "1.0",
            "D-" => "0.7",
            "F" => "0.0",
            _ => unreachable!(),
        }
    );
}

해시맵을 사용하는 게 개인적으로 더 마음에 드는 코드인 것 같습니다.

 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
use std::collections::HashMap;
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 grade = it.next().unwrap();

    let grade_to_score: HashMap<&str, f32> = HashMap::from([
        ("A+", 4.3),
        ("A0", 4.0),
        ("A-", 3.7),
        ("B+", 3.3),
        ("B0", 3.0),
        ("B-", 2.7),
        ("C+", 2.3),
        ("C0", 2.0),
        ("C-", 1.7),
        ("D+", 1.3),
        ("D0", 1.0),
        ("D-", 0.7),
        ("F", 0.0),
    ]);

    println!("{:.1}", grade_to_score[grade]);
}

hashmap 코드에서도 숫자를 문자열로 다루려고 시도하지는 않겠습니다…
그래도 함수로 뺀 버전은 한 번 보면 기분이 좋을 수 있을지도 모르니까 한 번 보고 가죠 ㅋㅋ!

 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
use std::io::{self, Read};

fn grade_to_score(grade: &str) -> f32 {
    match grade {
        "A+" => 4.3,
        "A0" => 4.0,
        "A-" => 3.7,
        "B+" => 3.3,
        "B0" => 3.0,
        "B-" => 2.7,
        "C+" => 2.3,
        "C0" => 2.0,
        "C-" => 1.7,
        "D+" => 1.3,
        "D0" => 1.0,
        "D-" => 0.7,
        "F" => 0.0,
        _ => unreachable!(),
    }
}

fn main() {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    let mut it = input.split_whitespace();

    let grade = it.next().unwrap();

    println!("{:.1}", grade_to_score(grade));
}

문자와 문자열

연산에 O(n)이 걸림이 잘 알려져 있지만 문제에서 여러 번 구하라는 말이 없으니 괜찮겠죠.

 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 s = it.next().unwrap();
    let i: usize = it.next().unwrap().parse().unwrap();

    println!("{}", s.chars().nth(i - 1).unwrap());
}

그대로 출력하기

그냥 모든 입력을 출력하면 됩니다.

1
2
3
4
5
6
7
8
use std::io::{self, Read};

fn main() {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).unwrap();

    println!("{}", input);
}

문자열

 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 t: usize = it.next().unwrap().parse().unwrap();
    for _ in 0..t {
        let s = it.next().unwrap();
        println!("{}{}", s.chars().next().unwrap(), s.chars().last().unwrap());
    }
}