码迷,mamicode.com
首页 > 其他好文 > 详细

2549 自然数和分解

时间:2016-06-10 21:37:48      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

 

 

2549 自然数和分解

标签:

原文地址:http://www.cnblogs.com/shenben/p/5574201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!