简单背包问题(实际上是子集问题)
2007-03-07 15:39
357 查看
从一堆物品weight[1],。。weight
中,从中选取若干件放入背包,使其重量恰好为s ,实际上对weight进行全排列,然后只要和为s,即可输出方案。
例如:
How many weight do you want to input..
6
Input 6 weight
3 4 6 5 7 2
Input the total Weight you want..
12
3+4+5 = 12
5+7 = 12
4+6+2 = 12
3+7+2 = 12
Press any key to continue
源程序:cpp文件:
#include "Knap.h"
void main()
{
vector<int>Weight;
int x;
int SUM;
int Count = 0;
cout<<"How many weight do you want to input.."<<endl;
cin>>Count;
cout<<"Input "<<Count<<" weight "<<endl;
for(int i = 0;i<Count;i++)
{
cin>>x;
Weight.push_back(x);
}
cout<<"Input the total Weight you want.."<<endl;
cin>>SUM;
Knap(Weight,Weight.size()-1,SUM);
}
头文件:Knap.h
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void Delete(vector<int>&Test,int j)
{
vector<int> Temp;
for(int i = 0;i<j;i++)
{
Temp.push_back(Test[i]);
}
for(i = j+1;i<Test.size();i++)
{
Temp.push_back(Test[i]);
}
while(!Test.empty())
{
Test.pop_back();
}
Test = Temp;
}
int sum(vector<int> Weight)
{
int sum = 0;
for(int i = 0;i<Weight.size();i++)
{
sum += Weight[i];
}
return sum;
}
//递归算法
void Knap(vector<int> Weight,int n,int SUM)
{
if(n < 0)
{
if(sum(Weight) == SUM)
{
for(int i = 0;i<Weight.size()-1;i++)
{
cout<<Weight[i]<<"+";
}
cout<<Weight[Weight.size()-1]<<" = "<<SUM;
cout<<endl;
}
}
else
{
vector<int>Temp;
Temp = Weight;
Delete(Weight,n);
Knap(Weight,n-1,SUM);
Weight = Temp;
Knap(Weight,n-1,SUM);
}
}
中,从中选取若干件放入背包,使其重量恰好为s ,实际上对weight进行全排列,然后只要和为s,即可输出方案。
例如:
How many weight do you want to input..
6
Input 6 weight
3 4 6 5 7 2
Input the total Weight you want..
12
3+4+5 = 12
5+7 = 12
4+6+2 = 12
3+7+2 = 12
Press any key to continue
源程序:cpp文件:
#include "Knap.h"
void main()
{
vector<int>Weight;
int x;
int SUM;
int Count = 0;
cout<<"How many weight do you want to input.."<<endl;
cin>>Count;
cout<<"Input "<<Count<<" weight "<<endl;
for(int i = 0;i<Count;i++)
{
cin>>x;
Weight.push_back(x);
}
cout<<"Input the total Weight you want.."<<endl;
cin>>SUM;
Knap(Weight,Weight.size()-1,SUM);
}
头文件:Knap.h
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void Delete(vector<int>&Test,int j)
{
vector<int> Temp;
for(int i = 0;i<j;i++)
{
Temp.push_back(Test[i]);
}
for(i = j+1;i<Test.size();i++)
{
Temp.push_back(Test[i]);
}
while(!Test.empty())
{
Test.pop_back();
}
Test = Temp;
}
int sum(vector<int> Weight)
{
int sum = 0;
for(int i = 0;i<Weight.size();i++)
{
sum += Weight[i];
}
return sum;
}
//递归算法
void Knap(vector<int> Weight,int n,int SUM)
{
if(n < 0)
{
if(sum(Weight) == SUM)
{
for(int i = 0;i<Weight.size()-1;i++)
{
cout<<Weight[i]<<"+";
}
cout<<Weight[Weight.size()-1]<<" = "<<SUM;
cout<<endl;
}
}
else
{
vector<int>Temp;
Temp = Weight;
Delete(Weight,n);
Knap(Weight,n-1,SUM);
Weight = Temp;
Knap(Weight,n-1,SUM);
}
}
相关文章推荐
- 简单背包问题(子集和)
- 0-1背包(最简单也是最难的背包问题)
- 简单:01背包问题
- 简单背包问题模板
- HDU 2546 饭卡 简单背包问题
- Dp基础 简单背包问题
- [转载]简单背包问题的递归算法和非递归算法
- 简单的背包问题
- 【多校连萌2】A 简单的背包问题【补题】
- 简单背包问题的递归与非递归实现
- CSUOJ 1945 最简单的题目 背包问题
- 最简单的0-1背包问题c++代码实例及运行结果
- 简单易懂的背包问题
- 简单背包(无价值总和)问题的实现。
- 简单:01背包问题
- 简单背包问题
- 简单的01背包问题(采药)
- 背包九讲第一讲-简单的0/1背包问题有感1.2
- 计算机调度算法 - NP-Hard问题 的 “背包问题” 的简单总结
- JAVA用栈求解(简单的)背包问题