[백준] 5597번 - 과제 안 내신 분..? [Java][C++]
[백준] 5597번 - 과제 안 내신 분..? [Java][C++]
1. 문제 풀이
$1$ 부터 $30$ 사이의 서로 다른 $28$ 개의 수가 주어졌을 때, 등장하지 않은 수를 찾아야 하는 문제다. 방문 체크 배열을 활용해서 해결했다.
2. 코드
1. 구현 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean[] visited = new boolean[1 + 30];
for (int i = 0; i < 28; i++) {
int n = Integer.parseInt(br.readLine());
visited[n] = true;
}
for (int i = 1; i <= 30; i++) {
if (visited[i]) continue;
System.out.println(i);
}
}
}
2. 구현 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<bool> visited(1 + 30);
for (int i = 0; i < 28; i++) {
int n;
cin >> n;
visited[n] = true;
}
for (int i = 1; i <= 30; i++) {
if (visited[i]) continue;
cout << i << '\n';
}
}
3. 풀이 정보
1. 구현 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 100 ms | 14172 KB | 501 B |
2. 구현 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 0 ms | 2020 KB | 259 B |
This post is licensed under CC BY 4.0 by the author.