Post

[BaekJoon] 5597번 - 과제 안 내신 분..? [Java][C++]

[BaekJoon] 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[] vis = new boolean[31];
        for (int i = 0; i < 28; i++) {
            int n = Integer.parseInt(br.readLine());
            vis[n] = true;
        }

        for (int i = 1; i <= 30; i++) {
            if (vis[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;

bool vis[31];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    for (int i = 0; i < 28; i++) {
        int n;
        cin >> n;
        vis[n] = true;
    }

    for (int i = 1; i <= 30; i++) {
        if (vis[i]) continue;

        cout << i << '\n';
    }
}

3. 디버깅


없음.


4. 참고


없음.


This post is licensed under CC BY 4.0 by the author.