[ 문제 ]
왕비를 피해 일곱 난쟁이들과 함께 평화롭게 생활하고 있던 백설공주에게 위기가 찾아왔다.
일과를 마치고 돌아온 난쟁이가 일곱 명이 아닌 아홉 명이었던 것이다.
아홉 명의 난쟁이는 모두 자신이 "백설 공주와 일곱 난쟁이"의 주인공이라고 주장했다.
뛰어난 수학적 직관력을 가지고 있던 백설공주는, 다행스럽게도 일곱 난쟁이의 키의 합이 100이 됨을 기억해 냈다.
아홉 난쟁이의 키가 주어졌을 때, 백설공주를 도와 일곱 난쟁이를 찾는 프로그램을 작성하시오.
[ 입력 ]
- 아홉 개의 줄에 걸쳐 난쟁이들의 키가 주어진다.
- 주어지는 키는 100을 넘지 않는 자연수이며, 아홉 난쟁이의 키는 모두 다르며, 가능한 정답이 여러 가지인 경우에는 아무거나 출력한다.
[ 출력 ]
- 일곱 난쟁이의 키를 오름차순으로 출력한다.
- 일곱 난쟁이를 찾을 수 없는 경우는 없다.
[ 예제 입력 ]
20
7
23
19
10
15
25
8
13
[ 예제 출력 ]
7
8
10
13
19
20
23
import java.io.*;
import java.util.Arrays;
public class Main {
public static void combination (int start, int cnt, int[] heights, int[] result) {
if (cnt == 7) {
int sum = 0;
for (int i = 0; i < 7; i++) sum+= result[i];
if (sum == 100) {
Arrays.sort(result);
for (int i = 0; i < 7; i++)
System.out.println(result[i]);
}
return;
}
for (int i = start; i < 9; i++) {
result[cnt] = heights[i];
combination(i+1, cnt+1, heights, result);
}
}
public static void main (String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] heights = new int[9];
for(int i = 0; i < 9; i++) heights[i] = Integer.parseInt(br.readLine());
int[] result = new int[7];
combination(0, 0, heights, result);
}
}
'Algorithm > 백준+프로그래머스+SWEA+정올+구름' 카테고리의 다른 글
[Algorithm] 백준 2605 줄 세우기 (0) | 2021.08.29 |
---|---|
[Algorithm] 백준 2578 빙고 (0) | 2021.08.29 |
[Algorithm] 백준 2527 직사각형 (0) | 2021.08.29 |
[Algorithm] 정올 1037 오류교정 (0) | 2021.08.28 |
[Algorithm] SWEA 6485 삼성시의 버스 노선 (0) | 2021.08.28 |