배열 두개를 비교하여 참/거짓을 가려내면 된다고 생각했으나,
배열 하나로 완료할 수 있는 과정이었다.
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] attended = new int[28];
int[] student = new int[31];
for(int i = 0; i<attended.length; i++){
int a = Integer.parseInt(br.readLine());
student[a] = 1;
}
for(int i = 1; i<student.length; i++){
if(student[i]!=1){
System.out.println(i);
}
}
}
}
하지만 생각해보니 attended 배열은 굳이 필요하지 않았다.
int[] student = new int[31];
for(int i = 0; i < 28; i++){
int a = Integer.parseInt(br.readLine());
student[a] = 1;
}
student배열을 생성해주고, 입력값 == 인덱스 값으로 하여 해당 배열의 성분은 1로 바꾼다.
입력값(1~30)을 int a 로 받아오기 때문에 student배열은 31까지 만들어준다.(인덱스가 30까지되게끔)
그래야 간편히 student[a] = 1; 로 바꿀 수 있다.
(배열이 생성될때는 기본값이 null 또는 0으로 생성된다)
그 뒤
for(int i = 1; i<student.length; i++){
if(student[i]!=1){
System.out.println(i);
}
}
해당 값이 1이 아닌 인덱스를 출력해주면 된다.
같은 방법이지만, boolean으로 만드는 방법이 있다.
(배열이 생성될때는 기본값이 null 또는 0으로 생성된다)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean[] arr = new boolean[31];
for (int i = 0; i < 28; i++) {
int n = Integer.parseInt(br.readLine());
arr[n] = true;
}
for (int i = 1; i <= 30; i++) {
if (!arr[i]) System.out.println(i);
}
}
}
'Programming > 알고리즘' 카테고리의 다른 글
[백준] 1546 - 평균 - JAVA[자바] (0) | 2022.11.27 |
---|---|
[백준] 3052 - 나머지 - JAVA[자바] (0) | 2022.11.27 |
[백준] 10871 - X보다 작은 수 - JAVA[자바] (0) | 2022.11.27 |
[백준] 10807 - 개수 세기 - JAVA[자바] (0) | 2022.11.27 |
[백준] 1110- 더하기 사이클 - JAVA[자바] (0) | 2022.11.26 |