[算法][递归] 整数划分 种类数

 

在整数n的所有不同划分中,将最大加数n1不大于m的划分个数记为q(n,m),则q的递推关系式为: 

 1 #include<iostream>
 2 using namespace std;
 3 int q(int n,int m){
 4     if((n<1)||(m<1))return 0;
 5     if((n==1)||(m==1))return 1;
 6     if(n<m)return q(n,n);
 7     if(n==m)return q(n,m-1)+1;
 8     return q(n,m-1)+q(n-m,m);
 9 }//递归函数
10 int main(){
11     int n;
12     cout<<"请输入整数 n = ";
13     while(cin>>n){
14         cout<<n<<" 的划分数量为 : "<<q(n,n)<<'\n';
15     }return 0;
16 }

 

 

posted @ 2014-03-26 11:05  beautifulzzzz  阅读(856)  评论(0编辑  收藏  举报