2549 自然数和分解

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 1
1 1 1 2
1 1 3
1 2 2
1 4
2 3

分类标签 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;
}

 

 
posted @ 2016-06-10 20:53  神犇(shenben)  阅读(239)  评论(0编辑  收藏  举报