문제풀이
아이스크림을 3가지 고른다고 했다. 고르는게 정해져있으니까 삼중 for문으로 모든 아이스크림을 넣어보고 만약 먹으면 안되는 아이스크림 조합이 들어가있므면 중단하고 다음으로 넘어가면 된다.
이중 배열을 선언해서 안 맞는 조합을 저장해야한다.
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int ans =0;
boolean[][] check = new boolean[201][201];
for(int i=0;i<m;i++){
st = new StringTokenizer(br.readLine());
int a=Integer.parseInt(st.nextToken());
int b=Integer.parseInt(st.nextToken());
check[a][b]=check[b][a]=true;
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
for(int k=j+1;k<=n;k++){
if(check[i][j]||check[i][k]||check[j][k]) continue;
ans++;
}
}
}
System.out.println(ans);
}
}'알고리즘 리뷰' 카테고리의 다른 글
| 백준 JAVA 2798 블랙잭 리뷰 dfs/3중for문 (0) | 2024.02.17 |
|---|---|
| 백준 JAVA 16439 치킨치킨치킨 리뷰 (0) | 2024.02.17 |
| 백준 JAVA 11725 트리의 부모 찾기 리뷰 (0) | 2024.02.17 |
| 프로그래머스 JAVA 120866 안전지대 (1) | 2024.02.16 |
| 백준 JAVA 5568 카드 놓기 리뷰 (0) | 2024.02.16 |