알고리즘

[JAVA] 백준 알고리즘_5597(과제 안 내신 분)***

줌인. 2024. 7. 1. 22:11

▶ 백준 알고리즘 5597

https://www.acmicpc.net/problem/5597

 

 

[문제]

X대학 M교수님은 프로그래밍 수업을 맡고 있다. 교실엔 학생이 30명이 있는데, 학생 명부엔 각 학생별로 1번부터 30번까지 출석번호가 붙어 있다.

교수님이 내준 특별과제를 28명이 제출했는데, 그 중에서 제출 안 한 학생 2명의 출석번호를 구하는 프로그램을 작성하시오.

 

 

[예제 입력]

입력은 총 28줄로 각 제출자(학생)의 출석번호 n(1 ≤ n ≤ 30)가 한 줄에 하나씩 주어진다. 출석번호에 중복은 없다.

3
1
4
5
7
9
6
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
9
30
6
12
10
20
21
11
7
5
28
4
18
29
17
19
27
13
16
26
14
23
22
15
3
1
24
25
 
[예제 출력]

출력은 2줄이다. 1번째 줄엔 제출하지 않은 학생의 출석번호 중 가장 작은 것을 출력하고, 2번째 줄에선 그 다음 출석번호를 출력한다.

10
2
8
 
 

[참고 코드 1_챗지피티1]

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    boolean[] submitted = new boolean[30];

    // 과제를 한 학생 번호 입력
    for (int i = 0; i < 28; i++) {
        int studentNumber = sc.nextInt();
        submitted[studentNumber - 1] = true; // 배열 인덱스는 0부터 시작하므로 -1
    }
    // 과제를 하지 않은 학생 출력
    for (int i = 0; i < 30; i++) {
        if (!submitted[i]) {
            System.out.println(i + 1);
        }
    }
    sc.close();
}

 

- 과제 제출 여부 추적 :

   boolean[] submitted = new boolean[30]; 배열을 사용하여 30명의 학생 중 과제를 제출한 학생을 추적

- 과제를 제출한 학생 번호 입력 :

  for (int i = 0; i < 28; i++) { ... } 루프에서 28명의 학생 번호를 입력받고, 해당 번호에 대응하는 배열 인덱스를 true로 설정

- 과제를 제출하지 않은 학생 찾기:

   for (int i = 0; i < 30; i++) { ... } 루프에서 배열을 순회하며 false인 인덱스를 찾아 해당 인덱스 + 1을 출력

 

 

 

[참고 코드 2_챗지피티2]

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    HashSet<Integer> submitted = new HashSet<>();
    // 과제를 한 학생 번호 입력
    for (int i = 0; i < 28; i++) {
        int studentNumber = sc.nextInt();
        submitted.add(studentNumber);
    }
    // 과제를 하지 않은 학생 출력
    for (int i = 1; i <= 30; i++) {
        if (!submitted.contains(i)) {
            System.out.println(i);
        }
    }
    sc.close();
}

 

- 과제 제출 여부 추적:

   HashSet<Integer> submitted = new HashSet<>();를 사용하여 30명의 학생 중 과제를 제출한 학생 번호를 저장

- 과제를 제출한 학생 번호 입력:

   for (int i = 0; i < 28; i++) { ... } 루프에서 28명의 학생 번호를 입력받고, submitted HashSet에 추가

- 과제를 제출하지 않은 학생 찾기:

   for (int i = 1; i <= 30; i++) { ... } 루프에서 1부터 30까지의 번호를 순회하며, HashSet에 포함되지 않은 번호를 출력

 

 

 

 

 

[참고 코드3_블로그]

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int[] student = new int[31];

    for(int i=1; i<29; i++) {
        int success = sc.nextInt();
        student[success] = 1;
    }
    for(int i=1; i<student.length; i++) {
        if(student[i]!=1)
            System.out.println(i);
    }
    sc.close();
}

- 1~28명의 목록 입력 및 입력 확인 된 인원 표기 '1'로

- 전체중 1로 표기되지 않은 학생 index 추출

※ 참고 : https://like-a-forest.tistory.com/48

 


[초기 코드]

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int [] ar = new int[30];
        //과제를 한 학생 번호 입력
        for(int i=0; i< ar.length-2; i++) {
            ar[i] = sc.nextInt();
        }

        int [] nAr = new int[2];
        for(int i=0; i<ar.length; i++) {
            for(int j=0; j<ar.length; j++) {
                if(ar[i] == j+1) {
                    break;
                }
            }
        }

        int min = ar[ar.length-2];
        int max = ar[ar.length-2];
        for(int i=ar.length-2; i<ar.length; i++) {
            if(min > ar[i]) {
                min = ar[i];
            }
            if (max < ar[i]) {
                max = ar[i];
            }
        }
        System.out.println(min + " " + max);
    }
}

- 의도가 무엇인지 알 수 없음

- 전체 중 1~30이 들어가있는지 유무를 판단하는 코드를 짜려했으나 알 수 없음

728x90