import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
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());
List<Integer> list = new ArrayList<>();
st = new StringTokenizer(br.readLine());
for(int i=0; i<n; i++) {
int num = Integer.parseInt(st.nextToken());
if(num < x) list.add(num);
}
for(int i=0; i < list.size() - 1; i++) {
System.out.print(list.get(i) + " ");
}
System.out.print(list.get(list.size() - 1));
br.close();
}
}
리스트에 값을 담고 출력한다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
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 num = Integer.parseInt(st.nextToken());
if(num < x) sb.append(num).append(" ");
}
System.out.println(sb);
br.close();
}
}
StringBuilder로 값을 담고 출력한다.
더 효율적인 방법
'Coding Test > 백준' 카테고리의 다른 글
[백준/자바] 10951 - A+B - 4 (0) | 2023.05.06 |
---|---|
[백준/자바] 25314 - 코딩은 체육과목 입니다 (0) | 2023.05.06 |
[백준/자바] 2480 - 주사위 세개 (0) | 2023.05.06 |
[백준/자바] 2525 - 오븐 시계 (0) | 2023.05.06 |
[백준/자바] 2884 - 알람 시계 (0) | 2023.05.06 |