Notice
Recent Posts
Recent Comments
«   2024/09   »
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
Archives
Today
Total
관리 메뉴

충분히 쌓여가는

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

IT/Java[백준]

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

빌드이너프 2023. 2. 26. 22:55

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

 

10871번: X보다 작은 수

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

www.acmicpc.net

문제풀이 1

import java.util.Scanner;
public class _10871_1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int X = sc.nextInt();

        int[] arr = new int[N];

        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        sc.close();

        for (int i = 0; i < N; i++) {
            if (arr[i] < X)
                System.out.print(arr[i] + " ");
        }
    }
}

배열 사용


문제풀이 2

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class _10871_2 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        int N = Integer.parseInt(st.nextToken());
        int X = Integer.parseInt(st.nextToken());

        StringBuilder sb = new StringBuilder();

        st = new StringTokenizer(br.readLine(), " ");
        for (int i = 0; i < N; i++) {
            int bi = Integer.parseInt(st.nextToken());
            if (bi < X)
                sb.append(bi + " ");
        }
        br.close();
        System.out.println(sb);
    }
}

배열 미사용


문제풀이 3

import java.util.Scanner;

public class _10871_3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int X = sc.nextInt();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < N; i++) {
            int bi = sc.nextInt();
            if (bi < X)
                sb.append(bi + " ");
        }
        sc.close();
        System.out.println(sb);

    }
}

배열 미사용

시간 제일 빠름