您的位置:首页 > 其它

数据组合

2005-04-15 11:27 323 查看
编写一个函数如下,要求返回从小于B的正整数中任选任意多个数加起来和等于A的组合数。要求写出递规算法。
int func(unsigned int A,unsigned int B)
==============================================================================
#include<iostream>
using namespace std;
int func(unsigned int A,unsigned int B)
{
if( (A<1) || (B<1) ) return 0;
if( (A==1) || (B==1) ) return 1;
if(A<B) return func(A, A);
if(A==B) return (func(A, B-1)+1);
return (func(A, B-1) + func(A-B, B));
}
int main()
{
int a,b;
cin>>a>>b;
cout<<func(a,b-1)<<endl;
system("PAUSE");
return 0;
}
来自一位网友的回复
=================================================================================
#include <iostream>
#include <cstdlib>
using namespace std;
int total=0;
void make(int a,int b,int start)
{
if(a==0){total++; return;}
if(a<0) return;
if(start==b-1) return;
int maxs =a/(++start);
int i=0;
for(int i=0;i<=maxs;i++)make(a-i*start,b,start);
}
int main(int argc, char *argv[])
{
int a,b,start=0;
cout<<"请输入a,b值"<<endl;
cin>>a>>b;
make(a,b,start);
cout<<total<<endl;
system("PAUSE");
return 0;
}
我自己的.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: