贪心 简单背包问题
2013-11-15 22:02
441 查看
背包问题的基本描述是: 有一个背包,能盛放的物品总重量为S,设有N件物品,其重量分别为w1,w2,...,wn.希望从N件物品中选择若干件物品,所选物品的重量之和恰能放入该背包,即所选物品的重量之和等于S。
程序1:递归算法
#include <stdio.h>
#include <stdlib.h>
int w[100];
int knap(int s,int n)
{
if(s==0)return 1;
if(s<0||(s>0&&n<1))return 0;
if(knap(s-w
,n-1))
{
printf("%d",w
);
return 1;
}
return knap(s,n-1);
}
int main()
{
int S,N,i;
while(~scanf("%d %d",&S,&N))
{
for(i=1;i<=N;i++)
scanf("%d",&w[i]);
if(knap(S,N))
printf("yes!\n");
else printf("no!\n");
}
return 0;
}
#include <iostream>
#include <stdlib.h>
using namespace std;
const int N=7;
const int S=20;
int w[N+1]={0,1,4,3,4,5,2,7};
int knap(int s,int n)
{
if(s==0)return 1;
if(s<0||(s>0&&n<1))return 0;
if(knap(s-w
,n-1))
{
cout<<w
;
return 1;
}
return knap(s,n-1);
}
int main(int argc, char *argv[])
{
if(knap(S,N))cout<<endl<<"OK"<<endl;
else cout<<"NO"<<endl;
system("PAUSE");
return 0;
}
程序2:非递归算法
#include <iostream>
#include <stdlib.h>
using namespace std;
const int N=7;
const int S=20;
int w[N+1]={0,1,4,3,4,5,2,7};
typedef struct{
int s;
int n;
int job;
}KNAPTP;
int knap(int s,int n)
{
KNAPTP stack[100],x;
int top,k,rep;
x.s=s;
x.n=n;
x.job=0;
top=1;
stack[top]=x;
k=0;
while(k==0&&top>0){
x=stack[top];
rep=1;
while(!k&&rep){
if(x.s==0)k=1;
else if(x.s<0||x.n<=0)rep=0;
else {
x.s=x.s-w[x.n--];
x.job=1;
stack[++top]=x;
}
}
if(!k){
rep=1;
while(top>=1&&rep){
x=stack[top--];
if(x.job==1){
x.s+=w[x.n+1];
x.job=2;
stack[++top]=x;
rep=0;
}
}
}
}
if(k){
while(top>=1){
x=stack[top--];
if(x.job==1)cout<<w[x.n+1];
}
}
return k;
}
int main(int argc, char *argv[])
{
if(knap(S,N))cout<<endl<<"OK"<<endl;
else cout<<"NO"<<endl;
system("PAUSE");
return 0;
}
程序1:递归算法
#include <stdio.h>
#include <stdlib.h>
int w[100];
int knap(int s,int n)
{
if(s==0)return 1;
if(s<0||(s>0&&n<1))return 0;
if(knap(s-w
,n-1))
{
printf("%d",w
);
return 1;
}
return knap(s,n-1);
}
int main()
{
int S,N,i;
while(~scanf("%d %d",&S,&N))
{
for(i=1;i<=N;i++)
scanf("%d",&w[i]);
if(knap(S,N))
printf("yes!\n");
else printf("no!\n");
}
return 0;
}
#include <iostream>
#include <stdlib.h>
using namespace std;
const int N=7;
const int S=20;
int w[N+1]={0,1,4,3,4,5,2,7};
int knap(int s,int n)
{
if(s==0)return 1;
if(s<0||(s>0&&n<1))return 0;
if(knap(s-w
,n-1))
{
cout<<w
;
return 1;
}
return knap(s,n-1);
}
int main(int argc, char *argv[])
{
if(knap(S,N))cout<<endl<<"OK"<<endl;
else cout<<"NO"<<endl;
system("PAUSE");
return 0;
}
程序2:非递归算法
#include <iostream>
#include <stdlib.h>
using namespace std;
const int N=7;
const int S=20;
int w[N+1]={0,1,4,3,4,5,2,7};
typedef struct{
int s;
int n;
int job;
}KNAPTP;
int knap(int s,int n)
{
KNAPTP stack[100],x;
int top,k,rep;
x.s=s;
x.n=n;
x.job=0;
top=1;
stack[top]=x;
k=0;
while(k==0&&top>0){
x=stack[top];
rep=1;
while(!k&&rep){
if(x.s==0)k=1;
else if(x.s<0||x.n<=0)rep=0;
else {
x.s=x.s-w[x.n--];
x.job=1;
stack[++top]=x;
}
}
if(!k){
rep=1;
while(top>=1&&rep){
x=stack[top--];
if(x.job==1){
x.s+=w[x.n+1];
x.job=2;
stack[++top]=x;
rep=0;
}
}
}
}
if(k){
while(top>=1){
x=stack[top--];
if(x.job==1)cout<<w[x.n+1];
}
}
return k;
}
int main(int argc, char *argv[])
{
if(knap(S,N))cout<<endl<<"OK"<<endl;
else cout<<"NO"<<endl;
system("PAUSE");
return 0;
}
相关文章推荐
- nyoj 106 背包问题(简单贪心)
- nyoj 106背包问题(贪心专题)
- 简单背包问题!
- 贪心法——活动选择问题和背包问题
- 算法分析与设计-15-背包问题的贪心算法
- 简单DP之最少硬币问题(多重背包问…
- 多重背包问题解法简单分析(POJ 1276为例)
- 贪心算法之背包问题
- 8.4.1(最优装载问题_简单贪心)
- NYOJ-106 背包问题(贪心)
- 贪心算法解决部分背包问题(C语言,ruby)
- 背包问题简单示例
- 算法分析与设计2017-2-背包问题的贪心算法
- 最简单的0-1背包问题c++代码实例及运行结果
- 51NOD - 贪心算法入门 - 活动安排问题 【简单贪心】
- 利用贪心算法解背包问题
- 简单背包问题的递归与非递归实现
- 背包问题之贪心学习
- Exercise(4):01背包(简单动规)问题的两种解法
- 采药(洛谷简单dp背包问题)