관리 메뉴

풀이 보관함

[RUST] 백준 10871번 : X보다 작은 수 본문

problem solving/백준

[RUST] 백준 10871번 : X보다 작은 수

viin 2024. 4. 25. 10:00

https://www.acmicpc.net/problem/10871

 

10871번: X보다 작은 수

첫째 줄에 N과 X가 주어진다. (1 ≤ N, X ≤ 10,000) 둘째 줄에 수열 A를 이루는 정수 N개가 주어진다. 주어지는 정수는 모두 1보다 크거나 같고, 10,000보다 작거나 같은 정수이다.

www.acmicpc.net

 

그냥 러스트 연습용으로 한번 풀어봤다.

 

러스트 입출력이 너무 힘들어서 사용자의 입력을 받는 용도로 쓰는 건 아닌 것 같다.

코드도 길고 알고리즘에서는 C++보다 매력있는 언어는 확실히 아닌 듯

 

 

절대로 내가 여러줄에 걸친 입력을 받다가 빡친게 아니고 .. 

절대로.. 

 

 

use std::io::{self, Write};

fn main() {

    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();
    let mut input = input.split_whitespace().map(|s| s.parse::<usize>().unwrap());
    let n = input.next().unwrap();
    let x = input.next().unwrap();

    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();
    let numbers = input.split_whitespace().map(|s| s.parse::<usize>().unwrap());

    for tmp in numbers {
        if x > tmp {
            print!("{} ", tmp);
        }
    }

    io::stdout().flush().unwrap();
}

/*
10 5
1 10 4 9 2 3 8 5 7 6
*/

 

 

참고로 C++로 풀면 깔끔하다!

#include <iostream>
int main(){
   
    std::ios::sync_with_stdio(false); std::cin.tie(NULL);
    
    int n, x, input;
    std::cin >> n >> x;
    
    for(int i=0; i<n; ++i)
    {
        std::cin >> input;
        if(input < x) 
        	std::cout << input << " ";
    }
    
    return 0;
}