X보다 작은 수
문제
내 풀이
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N,X;
String str;
str = br.readLine();
StringTokenizer st = new StringTokenizer(str);
N = Integer.parseInt(st.nextToken());
X = Integer.parseInt(st.nextToken());
int[] A = new int[N];
String str2 = br.readLine();
StringTokenizer sr = new StringTokenizer(str2);
for(int i = 0; i < N; i++)
{
A[i] = Integer.parseInt(sr.nextToken());
}
for(int i = 0; i < N; i++)
{
if(A[i] < X)
{
System.out.printf("%d ", A[i]);
}
}
}
}
다른 풀이
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
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 value = Integer.parseInt(st.nextToken());
if(value < X)
sb.append(value).append(' ');
}
System.out.println(sb);
}
}
출처
https://st-lab.tistory.com/38