문제
https://www.acmicpc.net/problem/10871
N개의 수 중에서 X보다 작은 수를 모두 출력하는 문제였다.
문제 풀이
1. Scanner
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
//배열의 크기 N과 비교할 값 X를 입력
int N = scanner.nextInt();
int X = scanner.nextInt();
int[] A = new int[N];
//배열 A의 요소를 입력
for (int i = 0; i < N; i++) {
A[i] = scanner.nextInt();
}
//X보다 작은 요소를 출력
for (int i = 0; i < N; i++) {
if (A[i] < X) {
System.out.print(A[i] + " ");
}
}
}
}
2. BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//배열의 크기 N과 비교할 값 X를 입력
String[] firstLine = reader.readLine().split(" ");
int N = Integer.parseInt(firstLine[0]);
int X = Integer.parseInt(firstLine[1]);
//배열 A의 요소를 입력
String[] elements = reader.readLine().split(" ");
StringBuilder result = new StringBuilder();
//X보다 작은 요소를 출력
for (int i = 0; i < N; i++) {
int value = Integer.parseInt(elements[i]);
if (value < X) {
result.append(value).append(" ");
}
}
//결과 출력
System.out.println(result.toString().trim());
}
}
'코딩테스트 > 백준' 카테고리의 다른 글
[백준/JAVA]3052번 나머지 (0) | 2025.01.10 |
---|---|
[백준/JAVA]5597번 과제 안 내신 분..? (0) | 2025.01.10 |
[백준/JAVA]10818번 최소, 최대 (0) | 2025.01.09 |
[백준/JAVA]10807번 개수 세기 (0) | 2025.01.09 |
[백준/JAVA]2884번 알람 시계 (0) | 2025.01.09 |