xjoi 1524 枚举集合
题目描述:
输入n,问从 1,2,3,...,n-1,n 中选出若干个数使得他们的和为 n ,有多少种方案。每一个数最多只能选一次。
输入格式:
一个数n
输出格式:
一个数表示答案
样例输入1:
2
样例输出1:
1
约定:
1<=n<=16
#include<bits/stdc++.h>
using namespace std;
int ans=0,n;
void dfs(int step,int s){//n指的是这个数选或者不选
if(step==n+1){
if(s==n){
ans++;
}
return;
}
for(int i=0;i<=1;i++){
dfs(step+1,s+step*i);
}
}
int main(){
cin>>n;
dfs(1,0);
cout<<ans;
return 0;
}