Skip to content

Commit 7e906ce

Browse files
committed
[Silver II] Title: 부분수열의 합, Time: 116 ms, Memory: 14228 KB -BaekjoonHub
1 parent 21bc28e commit 7e906ce

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Silver II] 부분수열의 합 - 1182
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1182)
4+
5+
### 성능 요약
6+
7+
메모리: 14228 KB, 시간: 116 ms
8+
9+
### 분류
10+
11+
브루트포스 알고리즘, 백트래킹
12+
13+
### 제출 일자
14+
15+
2026년 2월 28일 00:03:05
16+
17+
### 문제 설명
18+
19+
<p>N개의 정수로 이루어진 수열이 있을 때, 크기가 양수인 부분수열 중에서 그 수열의 원소를 다 더한 값이 S가 되는 경우의 수를 구하는 프로그램을 작성하시오.</p>
20+
21+
### 입력
22+
23+
<p>첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다.</p>
24+
25+
### 출력
26+
27+
<p>첫째 줄에 합이 S가 되는 부분수열의 개수를 출력한다.</p>
28+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static int[] numbers;
6+
static int N, S, totalCnt;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
StringTokenizer st = new StringTokenizer(br.readLine());
11+
12+
N = Integer.parseInt(st.nextToken());
13+
S = Integer.parseInt(st.nextToken());
14+
15+
numbers = new int[N];
16+
17+
st = new StringTokenizer(br.readLine());
18+
19+
for(int i=0; i<N; i++){
20+
numbers[i] = Integer.parseInt(st.nextToken());
21+
}
22+
23+
dfs(0, 0);
24+
25+
if(S == 0) totalCnt--;
26+
27+
System.out.println(totalCnt);
28+
}
29+
30+
static void dfs(int cnt, int sum){
31+
if(cnt == N){
32+
if(sum == S) totalCnt++;
33+
return;
34+
}
35+
36+
dfs(cnt+1, sum);
37+
dfs(cnt+1, sum+numbers[cnt]);
38+
}
39+
}

0 commit comments

Comments
 (0)