728x90
https://www.acmicpc.net/problem/15439
- 예제 입력1
1
- 예제 출력1
0
- 예제 입력2
2
- 예제 출력2
2
- 예제 입력3
5
- 예제 출력3
20
- 문제 접근
- 입력
- 첫째 줄 : N (1 <= N <= 2017)
- 출력
- 상의와 하의가 다른 서로 다른 색상의 조합 가짓수 출력
- 입력
- 문제 해결
- N * N의 2차원 배열로 나타냄
- N = 3 이라고 했을 때 i == j가 동일하면 0, 동일하지 않으면 1
- 배열의 총 합 = 서로 다른 색상의 조합 가짓수
- 기존 풀이 [메모리 : 30,676 KB / 시간 : 156 ms]
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[][] combination = new int[N][N];
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(i != j) combination[i][j]++;
}
}
int cnt = 0;
for(int i = 0; i < N; i++){
cnt += Arrays.stream(combination[i]).sum();
}
System.out.println(cnt);
}
728x90
'Coding Test > Step19. 조합론' 카테고리의 다른 글
[Java] 5단계. 다리 놓기 [1010번] (0) | 2024.11.12 |
---|---|
[Java] 4단계. 이항 계수 1 [10872번] (0) | 2024.11.12 |
[Java] 3단계. 팩토리얼 [10872번] (1) | 2024.11.12 |
[Java] 2단계. 녹색거탑 [24723번] (0) | 2024.11.11 |