2549 自然数和分解 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 把自然数N分解为若干个自然数之和,输出方案数。 输入描述 Input Description N,(1≤n≤50) 输出描述 Output Description 方案数 样例输入 Sample Input 5 样例输出 Sample Output 7 数据范围及提示 Data Size & Hint 5 可分为 1 1 1 1 11 1 1 21 1 31 2 21 42 35 分类标签 Tags 点此展开 深度优先搜索 搜索 #include<cstdio> #include<iostream> using namespace std; int a[1010],n,tot; void dfs(int x,int f){ for(int i=a[f-1];i<=x;i++){//从前一个开始 if(i<n){ a[f]=i; x-=i;//挨个拆分 if(x==0){ ++tot;return; } dfs(x,f+1);//将拆分后的x与下一个f 继续进行拆分 x+=i; } } } int main(){ scanf("%d",&n); a[0]=1; dfs(n,1); cout<<tot+1<<endl;//加上本身 return 0; } 相关文章: 2021-07-04 2021-10-26 2021-06-01 2021-10-21