数据组合
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;
}
我自己的.
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;
}
我自己的.
相关文章推荐
- 大数据架构:flume-ng+Kafka+Storm+HDFS 实时系统组合
- Java——多态,引用数据类型强转,组合模式
- 组合数据浅析之“幻方”
- SQLSERVER通过游标查询两个数据表共有字段名组合成的字符串
- 百度2014校园招聘算法——给出一组数据A=[a_0, a_1, a-2, ... a_n](其中n可变),打印出该数值元素的所有组合。
- [vba]excel中求选中数据和为给定数所有的组合
- 对一组数据做无序排列(组合)的简单实现
- 大数据架构:flume-ng+Kafka+Storm+HDFS 实时系统组合
- Mysql数据分组GROUP BY 和HAVING,与WHERE组合使用
- Struts2 多条件组合查询和准备默认数据
- Struts2 多条件组合查询和准备默认数据
- Oracle 学习:PL/SQL循序渐进全面学习教程--课程十三 使用组合数据类型* 游标操纵数据
- SQL学习笔记5 组合查询 插入/更新/删除数据 创建和操纵表
- tbar和btn数据组合写tbar的效果还有其分割线的实现
- 用Python组合Celery Redis RabbitMQ进行分布式数据抓取
- [lua, mysql] 将多条记录数据组合成一条sql插入语句(for mysql)
- [置顶] AChartEngine高级应用之CombinedXYChart(组合统计图)在单个统计图上显示多样化的数据
- python 组合数据类型
- 大数据架构:flume-ng+Kafka+Storm+HDFS 实时系统组合
- 8.4 组合数据和行为