2010年中兴面试题 01背包问题算法
来源:互联网 发布:网络可视对讲系统 编辑:程序博客网 时间:2024/04/30 15:28
题目:
编程求解:
输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,
使其和等于 m ,要求将其中所有的可能组合列出来。
程序:
#include <list>
#include <iostream>
using namespace std;
void qiujie(int sum,int n)
{
static list<int> ilist;
if (sum < 1 || n < 1)
return;
if (sum > n)
{
F_LOOP:
ilist.push_front(n);
qiujie(sum-n,n-1);
ilist.pop_front();
qiujie(sum,n-1);
}else{
for(list<int>::reverse_iterator it = ilist.rbegin();it != ilist.rend();it++)
cout<<*it<<" + ";
cout<<sum;
cout<<endl;
while( (--n) >0)
{
if (sum > n)
goto F_LOOP;
}
}
}
int main()
{
int sum,n;
cout<<"请输入你要等于多少的数据sum:"<<endl;
cin>>sum;
cout<<"请输入你要从1......n数列中取值的n:"<<endl;
cin>>n;
cout<<"生成所有的排序序列: "<<endl;
qiujie(sum,n);
return 0;
}
运行结果:
(1) sum < n的情况
(2) sum > n的情况
(3) sum = n的情况
- 2010年中兴面试题 01背包问题算法
- 2010年中兴面试题
- 2010年中兴面试题
- 2010年中兴面试题
- 21、2010年中兴面试题
- 15.2010 年中兴面试题
- 中兴面试题 01背包问题
- 九章算法面试题58 背包问题
- 九章算法面试题59 背包问题II
- 面试题:简单背包问题
- 【面试题】简单背包问题
- 背包算法解决一道华为面试题
- 面试题34:简单背包问题
- 九章算法 | Facebook 面试题 : Backpack VI 背包算法
- [算法]01背包问题
- 【算法】01背包问题
- 百度面试题 0-1背包问题-动态规划法
- 2014年中兴笔试试题
- C# ASP.NET--------高手进阶篇:右键菜单管理
- 用五味滋养五脏
- 栈的复习
- 变频器工作原理
- FPGA协议的写法
- 2010年中兴面试题 01背包问题算法
- Online Judge上陪审团选人问题用Java实现的一个AC解
- VC使用makefile小结
- 添加一行
- 拦截器的基本使用方法
- IE6支持PNG透明(alpha通道)的4种方法
- poj 3670
- vi
- .h头文件、 .lib库文件、 .dll动态链接库文件之间的关系