자바 5597번

소만이·2023년 11월 15일

자바 백준문제

목록 보기
1/4

문제 5597번

아래는 내가 작성한 코드이다.

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int[] studentList = new int[30];

        for (int i = 0; i < studentList.length; i++) {
            studentList[i] = i + 1;
        }

        int[] submitList = new int[28];
        int[] noList = new int[2];
        for (int i = 0; i < submitList.length; i++) {
            int student = Integer.parseInt(br.readLine());
            submitList[i] = student;
        }

        boolean submit = false;
        int count = 0;
        for (int std : studentList) {
            if(count > 1) break;
            for (int submitstd : submitList) {
                if (std == submitstd) {
                    submit = true;
                    break;
                } else {
                    submit = false;
                }
            }
            if (!submit) {
                    noList[count] = std;
                    count++;
            }
        }


        for (int i = 0; i < noList.length; i++) {
            int temp = noList[i];
            if (noList[i] > noList[i + 1]) {
                noList[i] = noList[i + 1];
                noList[i + 1] = temp;
            }else{
                break;
            }
        }
        for (int i : noList) {
            System.out.println(i);
        }
    }

이렇게 풀었으나 뭔가 복잡하게 한 느낌이 들어 다른 사람들은 어떻게 풀었는지 검색해보았다.

역시나.. 이렇게 복잡하게 할 것이 아니었다!!
다른 사람들이 한 것을 보고 뒷통수를 맞은 느낌이었다..
이렇게 간단하게 할 수 있는 것을!!!

학생 리스트에서 제출한 사람들은 1 / 제출 안한 사람들은 0 으로 바꿔,
제출하지 않은 사람들만 뽑아내면 되는 문제였던 것이다!
굳이 제출하지 않은 사람들의 리스트를 받아 순서를 바꿀 필요도 없이 int[] 인덱스 값을 활용해 작은 순서대로 뽑을 수 있었는데 너무 복잡하게 생각했던 것 같다.

아래는 바꾼 코드이다.

  public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int[] students = new int[30];

        for(int i = 0; i < 28; i++) {
            int submit = Integer.parseInt(br.readLine());
            students[submit-1] = 1;
        }
       
        for(int i = 0; i < students.length; i++){
            if(students[i] !=1) System.out.println(i+1);
        }
    }

복잡하게 생각하지 말고 단순하게 코드를 짜보자..!!!ㅠㅠ

0개의 댓글