X보다 작은 수

  1. 배열은 안 썼지만 풀었죠?
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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 n: usize = it.next().unwrap().parse().unwrap();
    let x: u16 = it.next().unwrap().parse().unwrap();

    for _ in 0..n {
        let a: u16 = it.next().unwrap().parse().unwrap();
        if a < x {
            print!("{a} ");
        }
    }
}
  1. 그래도 배열 새싹 문제니까 배열을 써봤습니다.
 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 n: usize = it.next().unwrap().parse().unwrap();
    let x: u16 = it.next().unwrap().parse().unwrap();

    let mut a: Vec<u16> = Vec::new();
    for _ in 0..n {
        a.push(it.next().unwrap().parse().unwrap());
    }

    for i in 0..n {
        if a[i] < x {
            print!("{} ", a[i])
        }
    }
}
  1. 엄… Vec이 a가 아니라 a에서 x보다 작은 값들이니까 변수명을 바꿀 필요가 있을 것 같은데 생각이 안 나네요
 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 n: usize = it.next().unwrap().parse().unwrap();
    let x: u16 = it.next().unwrap().parse().unwrap();

    let a: Vec<u16> = (0..n)
        .map(|_| it.next().unwrap().parse().unwrap())
        .filter(|&a_i| a_i < x)
        .collect();

    for a_i in &a {
        print!("{} ", a_i);
    }
}
  1. 으어억
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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 n: usize = it.next().unwrap().parse().unwrap();
    let x: u16 = it.next().unwrap().parse().unwrap();

    print!(
        "{}",
        (0..n)
            .map(|_| it.next().unwrap().parse::<u16>().unwrap())
            .filter(|&a_i| a_i < x)
            .map(|a_i| a_i.to_string())
            .collect::<Vec<_>>()
            .join(" ")
    );
}
  1. 음…
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 n: usize = it.next().unwrap().parse().unwrap();
    let x: u16 = it.next().unwrap().parse().unwrap();

    let a: Vec<u16> = (0..n)
        .map(|_| it.next().unwrap().parse().unwrap())
        .collect();

    let result = a
        .iter()
        .filter(|&&a_i| a_i < x)
        .map(|a_i| a_i.to_string())
        .collect::<Vec<_>>();

    print!("{}", result.join(" "));
}
  1. 마지막으로 한 번
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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 n: usize = it.next().unwrap().parse().unwrap();
    let x: u16 = it.next().unwrap().parse().unwrap();

    let result: Vec<String> = (0..n)
        .map(|_| it.next().unwrap().parse::<u16>().unwrap())
        .filter(|&a_i| a_i < x)
        .map(|a_i| a_i.to_string())
        .collect();

    print!("{}", result.join(" "));
}
  1. 으어어어억
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 n: usize = it.next().unwrap().parse().unwrap();
    let x: u16 = it.next().unwrap().parse().unwrap();

    let a: Vec<u16> = (0..n)
        .map(|_| it.next().unwrap().parse().unwrap())
        .collect();

    let result = a.iter().filter(|&&a_i| a_i < x);

    print!(
        "{}",
        result
            .map(|a_i| a_i.to_string())
            .collect::<Vec<_>>()
            .join(" ")
    );
}
  1. 끄억
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 n: usize = it.next().unwrap().parse().unwrap();
    let x: u16 = it.next().unwrap().parse().unwrap();

    let a = (0..n).map(|_| it.next().unwrap().parse::<u16>().unwrap());
    let result: Vec<_> = a.filter(|&a_i| a_i < x).collect();

    print!(
        "{}",
        result
            .iter()
            .map(|v| v.to_string())
            .collect::<Vec<_>>()
            .join(" ")
    );
}

개수 세기

간단하게 넘어가죠

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

    let a: Vec<i8> = (0..n)
        .map(|_| it.next().unwrap().parse().unwrap())
        .collect();
    let v: i8 = it.next().unwrap().parse().unwrap();

    println!("{}", a.iter().filter(|&&x| x == v).count())
}

과제 안 내신 분..?

과제를 내는 건 중요합니다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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: Vec<u8> = (0..28)
        .map(|_| it.next().unwrap().parse().unwrap())
        .collect();

    for i in 1..=30 {
        if !a.contains(&i) {
            println!("{i}")
        }
    }
}

순서가 중요하지 않으니 그냥 bool 배열로 저장하는 편이 보기에 좋을지도 모르겠습니다.
0번 항목이 비어있다는 것은 (지금은 상관 없지만) 기억할 만합니다.
한 수를 찾고 1+2+…+29+30에 입력받은 모든 수랑 찾은 한 수를 뺄 수도 있겠네요 ㅋㅋ!

 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 mut submitted = [false; 31];
    for _ in 0..28 {
        let student: usize = it.next().unwrap().parse().unwrap();
        submitted[student] = true;
    }

    for i in 1..=30 {
        if !submitted[i] {
            println!("{i}");
        }
    }
}

행렬 덧셈

행렬을 더해요

 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
32
33
34
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 n: usize = it.next().unwrap().parse().unwrap();
    let m: usize = it.next().unwrap().parse().unwrap();

    let mut a: Vec<Vec<i16>> = vec![vec![0; m]; n];
    for i in 0..n {
        for j in 0..m {
            a[i][j] = it.next().unwrap().parse().unwrap();
        }
    }
    let mut b: Vec<Vec<i16>> = vec![vec![0; m]; n];
    for i in 0..n {
        for j in 0..m {
            b[i][j] = it.next().unwrap().parse().unwrap();
        }
    }

    for i in 0..n {
        println!(
            "{}",
            (0..m)
                .map(|j| a[i][j] + b[i][j])
                .map(|v| v.to_string())
                .collect::<Vec<_>>()
                .join(" ")
        )
    }
}