250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 에라토스테네스의채
- 완전탐색
- 볼륨 만들기
- 정보처리기사
- 중복순열
- 백준15652
- BFS
- 파티션 크기 조정
- 자바
- 코테준비
- 알고리즘
- 삼성역테
- 정올 1620
- N과M
- 중복조합
- 완탐
- D드라이브생성
- 백준
- java
- 순열
- 재귀함수
- 백준2251
- 알고리즘개념
- Bfs와DFS
- 자바 코테
- 23288
- 전화번호속의암호
- 백준13458
- 코테
- 주사위굴리기2
Archives
- Today
- Total
뚱땅뚱땅
[문제] 백준 1182번 부분수열의 합 본문
728x90
풀이
부분집합을 다 구하면 된다.
주의할 점은 S = 0인 경우 공집합도 가능하니까 이때는 total을 하나 빼야 한다.
public class BOJ_1182 {
static int N, S;
static int[] numbers;
static int total = 0;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine()," ");
N = Integer.parseInt(st.nextToken());
S = Integer.parseInt(st.nextToken());
numbers = new int[N];
st = new StringTokenizer(in.readLine()," ");
for(int i=0;i<N;i++) {
numbers[i] = Integer.parseInt(st.nextToken());
}
subset(0, new boolean[N]);
if(S == 0)
System.out.println(total-1);
else
System.out.println(total);
}
static void subset(int cnt, boolean[] visited) {
if(cnt == N) {
int sum = 0;
for(int i =0;i<N;i++) {
if(visited[i]) {
sum += numbers[i];
}
}
if(sum == S) total++;
return;
}
visited[cnt] = true;
subset(cnt+1, visited);
visited[cnt] = false;
subset(cnt+1, visited);
}
}
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[문제] 백준 2961번 도영이가 만든 맛있는 음식 (0) | 2021.02.15 |
---|---|
[문제] 백준 14225번 부분수열의 합 (0) | 2021.02.15 |
[문제] 백준 2609번 최대공약수와 최소공배수 (0) | 2021.02.15 |
[문제] 백준 1759번 암호 만들기 (0) | 2021.02.14 |
[문제] 백준 15900번 나무 탈출 (0) | 2021.02.14 |
Comments