YTU.3131: 进阶递归之简单的整数划分问题

来源:互联网 发布:java面向过程实现加法 编辑:程序博客网 时间:2024/06/05 18:20

3131: 进阶递归之简单的整数划分问题

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 35  Solved: 23
[Submit][Status][Web Board]

Description

将正整数n 表示成一系列正整数之和,n=n1+n2+…+nk, 其中n1>=n2>=…>=nk>=1 ,k>=1 。
正整数n 的这种表示称为正整数n 的划分。正整数n 的不同的划分个数称为正整数n 的划分数。

Input

标准的输入包含若干组测试数据。每组测试数据是一个整数N(0 < N <= 50)。

Output

对于每组测试数据,输出N的划分数。

Sample Input

5

Sample Output

7

HINT


5, 4+1, 3+2, 3+1+1, 2+2+1, 2+1+1+1, 1+1+1+1+1

AC代码:

#include <stdio.h>int cnt=0;int fx(int n,int x)//n记录整数的剩余数,x记录上一步的数{    if(n==0)//分配完了    {        cnt++;        return ;    }    int i=n<x? n:x;//i取大的    for(i; i>0; i--)        fx(n-i,i);}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        cnt=0;        fx(n,n);        printf("%d\n",cnt);    }    return 0;}
参考文章:https://www.cnblogs.com/cxmhy/p/4464060.html