codevs 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

bfs 
#include <cstdio>

int N,ans,i;
void dfs(int sum,int pos)
{
    if(sum==0)
    {
        ans++;
        return;
    }
    for(int i=pos;i<=sum;i++)
    dfs(sum-i,i);
}
int main()
{
    scanf("%d",&N);
    dfs(N,1);
    printf("%d",ans);
    return 0;
}
 
我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
原文地址:https://www.cnblogs.com/ruojisun/p/6476581.html