반응형
1. Scanner를 이용한 방법
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] student = new int[30];
for(int i=0; i<28; i++){
int num = sc.nextInt();
student[num-1] = 1;
}
for(int j=0; j<student.length; j++){
if(student[j] != 1){
System.out.println(j+1);
}
}
sc.close();
}
}
import java.util.Scanner;
public class Main{
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 num = sc.nextInt();
student[num] = 1;
}
for(int j=1; j<student.length; j++){
if(student[j] != 1){
System.out.println(j);
}
}
sc.close();
}
}
2. BufferedReader를 이용한 방법
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] student = new int[30];
for(int i=0; i<28; i++){
int num = Integer.parseInt(br.readLine());
student[num-1] = 1;
}
for(int j=0; j<student.length; j++){
if(student[j] != 1){
System.out.println(j+1);
}
}
br.close();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] student = new int[31];
for(int i=1; i<29; i++){
int num = Integer.parseInt(br.readLine());
student[num] = 1;
}
for(int j=1; j<student.length; j++){
if(student[j] != 1){
System.out.println(j);
}
}
br.close();
}
}
두 방법 2가지가 있는데 차이는 int[] student = new int[30] 이냐 int[] student = new int[31]이냐의 차이다.
30일 때는 반복문을 0부터 시작하여 28 미만으로 끝나고 0부터 시작하므로 [num-1]을 해주고 나중에 출력 시에 출석번호는 1부터 시작하기 때문에 다시 [j+1]을 해준다.
31일 때는 반복문을 1부터 시작하여 29미만으로 끝나고 [num] 을 그대로 사용하고 출력 시에도 변화 없이 그대로 j를 출력해준다.
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 10811 - 바구니 뒤집기 (0) | 2024.02.19 |
---|---|
[백준] 3052 - 나머지 (0) | 2024.02.18 |
[백준] 10813 - 공 바꾸기 (0) | 2024.02.16 |
[백준] 10810 - 공 넣기 (0) | 2024.02.15 |
[백준] 2562 - 최댓값 (0) | 2024.02.14 |