Algorithm/๋ฐฑ์ค
๋ฐฑ์ค 3040๋ฒ - ๋ฐฑ์ค ๊ณต์ฃผ์ ์ผ๊ณฑ ๋์์ด
giraffe_
2022. 8. 11. 17:49
https://www.acmicpc.net/problem/3040
3040๋ฒ: ๋ฐฑ์ค ๊ณต์ฃผ์ ์ผ๊ณฑ ๋์์ด
๋งค์ผ ๋งค์ผ ์ผ๊ณฑ ๋์์ด๋ ๊ด์ฐ์ผ๋ก ์ผ์ ํ๋ฌ ๊ฐ๋ค. ๋์์ด๊ฐ ์ผ์ ํ๋ ๋์ ๋ฐฑ์ค๊ณต์ฃผ๋ ๊ทธ๋ค์ ์ํด ์ ๋ ์์ฌ๋ฅผ ์ค๋นํ๋ค. ๋ฐฑ์ค๊ณต์ฃผ๋ ์์ ์ผ๊ณฑ๊ฐ, ์ ์ ์ผ๊ณฑ๊ฐ, ๋์ดํ ์ผ๊ณฑ๊ฐ๋ฅผ ์ค๋นํ๋ค.
www.acmicpc.net
9๋ช ์ค์์ 7๋ช ์ ์ฐพ๋ ๊ฒ์ด๋ฏ๋ก 9C7 -> ์กฐํฉ ๋ฌธ์ ์ด๋ค.
์กฐํฉ 7๊ฐ๋ฅผ ๋ค ์์ฑํ๊ณ ๋ฆฌํดํ๊ธฐ ์ ์ ์กฐํฉ์ ํฉ์ด 100์ด ๋๋์ง ํ์ธํด์ ์ถ๋ ฅํด์ฃผ๋ฉด ๋๋ค.
์ฝ๋
import java.util.Scanner;
public class Main {
static int[] hats;
static boolean[] visited;
static int[] result;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
hats = new int[9];
for(int i = 0; i < 9; i++) {
hats[i] = sc.nextInt();
}
visited = new boolean[9];
result = new int[7];
combination(0, 0);
}
public static void combination(int start, int depth) {
if(depth == 7) {
int sum = 0;
for(int n : result) {
sum += n;
}
if(sum == 100) {
for(int n : result) {
System.out.println(n);
}
}
return;
}
for(int i = start; i < 9; i++) {
if(visited[i]) continue;
visited[i] = true;
result[depth] = hats[i];
combination(i + 1, depth + 1);
visited[i] = false;
}
}
}