[백준] 10871번 - X보다 작은 수 [Java][C++]
[백준] 10871번 - X보다 작은 수 [Java][C++]
1. 문제 풀이
수열 $A$ 에서 $X$ 보다 작은 수를 모두 출력하는 문제로 비교 연산자를 활용하면 간단하게 해결할 수 있다.
2. 코드
1. 구현 [Java]
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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int X = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
for (int n : arr) {
if (n < X) sb.append(n).append(" ");
}
System.out.println(sb);
}
}
2. 구현 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int& x : v) cin >> x;
for (int x : v) {
if (x < k) {
cout << x << ' ';
}
}
}
3. 풀이 정보
1. 구현 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 140 ms | 15384 KB | 733 B |
2. 구현 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 0 ms | 2180 KB | 295 B |
This post is licensed under CC BY 4.0 by the author.