728x90
๐๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/17610
๐ป์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int k = Integer.parseInt(br.readLine());
int[] weights = new int[k];
int s = 0;
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < k; i++) {
weights[i] = Integer.parseInt(st.nextToken());
s += weights[i];
}
boolean[] isPossible = new boolean[s + 1];
getBalance(0, 0, weights, isPossible, k);
int answer = 0;
for (int i = 1; i < s; i++) {
if (!isPossible[i]) answer++;
}
System.out.println(answer);
}
private static void getBalance(int index, int weight, int[] weights, boolean[] isPossible, int k) {
if (index == k) {
if (weight >= 0) isPossible[weight] = true;
return;
}
getBalance(index + 1, weight, weights, isPossible, k);
getBalance(index + 1, weight + weights[index], weights, isPossible, k);
getBalance(index + 1, weight - weights[index], weights, isPossible, k);
}
}
728x90
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Algorithm/Baekjoon] ๋๋ฌด ์๋ฅด๊ธฐ - 14247(S2/JAVA) (1) | 2024.11.11 |
|---|---|
| [Algorithm/Baekjoon] ์ ์ ์ - 12893 (G4/JAVA) (1) | 2024.11.04 |
| [Algorithm/Baekjoon] ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ - 2667 (S1/JAVA) (0) | 2024.10.21 |
| [Algorithm/Baekjoon] ๋ก ๋๋ฆฌ๊ธฐ - 20007 (S1/JAVA) (0) | 2024.10.07 |
| [Algorithm/Baekjoon] ์ค๋ฅด๋ง ์ - 11057 (S1/JAVA) (0) | 2024.09.30 |