728x90
๐๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/1182
๐ป์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int answer = 0;
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 R = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
getSum(0, N, R, arr, 0);
System.out.println(R == 0 ? answer - 1 : answer);
}
private static void getSum(int depth, int n, int r, int[] arr, int sum) {
if (depth == n) {
if (sum == r) {
answer++;
}
return;
}
getSum(depth + 1, n, r, arr, sum + arr[depth]);
getSum(depth + 1, n, r, arr, sum);
}
}
728x90
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Algorithm/Baekjoon] ํ์ ๋ง์ถ๊ธฐ - 11509 (G5/JAVA) (0) | 2024.09.26 |
|---|---|
| [Algorithm/Baekjoon] ๊ฑฐ์ ์์ - 1456 (G5/JAVA) (0) | 2024.09.09 |
| [Algorithm/Baekjoon] ์์ฐ - 2512 (S2/JAVA) (3) | 2024.03.20 |
| [Algorithm/Baekjoon] ์ง๋ฆ๊ธธ - 1446 (S1/JAVA) (2) | 2023.09.12 |
| [Algorithm/Baekjoon] ์๋ํฐ - 1406 (S2/JAVA) (1) | 2023.08.08 |