[알고리즘]/백준
자바 - 구현 - 백준 1453 피시방 알바
broship
2021. 3. 2. 10:54
문제
문제해결
import java.util.Scanner;
public class B1454_2 {
public static void main(String[] args) {
boolean[] check = new boolean[101];
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int cnt = 0;
for(int i=0;i<size;i++) {
int tmp = sc.nextInt();
if(check[tmp]==true)
cnt++;
else
check[tmp] = true;
}
System.out.println(cnt);
}
}
- 1번부터 100번까지 자리 체크를 할수 있는 boolean 배열을 만든다
- 들어온 사람이 자리를 차지하면 true로 바꾼다
- 이미 자리가 있다면 cnt를 1씩 증가시킨다
- cnt가 거절당한 사람의 수이다
import java.util.Scanner;
public class B1453 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int[] humans = new int[size];
for(int i=0;i<size;i++)
humans[i] = sc.nextInt();
int cnt = 0;
for(int i=0;i<size;i++) {
int tmp = humans[i];
for(int j=i+1;j<size;j++) {
if(tmp==humans[j]) {
cnt++;
break;
}
}
}
System.out.println(cnt);
}
}
- 이중 반복문을 사용해 들어온 값이 이미 배열안에 있나 확인하는 방법으로도 풀이가 가능하다