[문제]
https://www.acmicpc.net/problem/1182
1182번: 부분수열의 합
첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다.
www.acmicpc.net
[풀이]
백트래킹 문제이다.
[코드]
#include <iostream>
using namespace std;
int n,s;
int arr[30];
int cnt;
void func(int cur, int tot){
if(cur == n)
{
if(tot == s) cnt++;
return;
}
func(cur+1, tot);
func(cur+1, tot+arr[cur]);
}
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for(int i=0; i<n; i++)
cin >> arr[i];
func(0,0);
if(s==0) cnt--;
cout << cnt;
}
'알고리즘 > 풀이' 카테고리의 다른 글
백준 15650 - N과 M (2) - C++ (1) | 2022.01.13 |
---|---|
백준 9663번 N-Queen / C++ (0) | 2022.01.09 |
백준 15649번 - N과 M(1) - C++ (0) | 2022.01.08 |
백준 2447번 - 별 찍기 - 10 (C++) (0) | 2022.01.02 |
1992번 - 쿼드트리 (C++) (0) | 2021.12.30 |