본문 바로가기
코딩테스트/백준

[백준/5597] 과제 안 내신 분..? (Java)

by wo__ongii 2024. 12. 27.
728x90

1. 배열로 풀기

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Day21 {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] arr = new int[30];
        for(int i=0;i<28;i++){
            int num = Integer.parseInt(br.readLine());
            arr[num-1] = 1;
        }
        for(int i=0;i<30;i++){
            if(arr[i] == 0){
                System.out.println(i+1);
            }
        }
    }
}

 

2.  List로 풀기

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Day21 {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        List<Integer> nums = new ArrayList<>();
        //list에 넣어놓기
        for(int i=0;i<28;i++){
            int num = Integer.parseInt(br.readLine());
            nums.add(num);
        }
        //오름차순 정렬
        Collections.sort(nums);
        
        //contains()로 값 포함 여부 체크
        for(int i=1;i<=30;i++){
            if(!nums.contains(i)){
                bw.write(i+"\n");
            }
        }
        br.close();
        bw.flush();
        bw.close();
    }
}
728x90
반응형