贪心方法:总是对当前的问题作最好的选择,也就是局部寻优。最后得到整体最优。

应用:1:该问题可以通过“局部寻优”逐步过渡到“整体最优”。贪心选择性质与“动态规划”的主要差别。

2:最优子结构性质:某个问题的整体最优解包含了“子”问题的最优解。

程序1

 #include <iostream.h>
struct goodinfo
{
float p; //物品效益
float w; //物品重量
float X; //物品该放的数量
int flag; //物品编号
};//物品信息结构体 void Insertionsort(goodinfo goods[],int n)
{
int j,i;
for(j=;j<=n;j++)
{
goods[]=goods[j];
i=j-; while (goods[].p>goods[i].p)
{
goods[i+]=goods[i];
i--;
}
goods[i+]=goods[];
}
}//按物品效益,重量比值做升序排列 void bag(goodinfo goods[],float M,int n)
{ float cu;
int i,j;
for(i=;i<=n;i++)
goods[i].X=;
cu=M; //背包剩余容量
for(i=;i<n;i++)
{
if(goods[i].w>cu)//当该物品重量大与剩余容量跳出
break; goods[i].X=;
cu=cu-goods[i].w;//确定背包新的剩余容量
}
if(i<=n)
goods[i].X=cu/goods[i].w;//该物品所要放的量
/*按物品编号做降序排列*/
for(j=;j<=n;j++)
{
goods[]=goods[j];
i=j-; while (goods[].flag<goods[i].flag)
{
goods[i+]=goods[i];
i--;
}
goods[i+]=goods[];
}
///////////////////////////////////////////
cout<<"最优解为:"<<endl;
for(i=;i<=n;i++)
{
cout<<"第"<<i<<"件物品要放:";
cout<<goods[i].X<<endl;
}
} void main()
{
cout<<"|--------运用贪心法解背包问题---------|"<<endl;
cout<<"|---power by zhanjiantao(028054115)---|"<<endl;
cout<<"|-------------------------------------|"<<endl;
int j;
int n;
float M;
goodinfo *goods;//定义一个指针
while(j)
{
cout<<"请输入物品的总数量:";
cin>>n;
goods=new struct goodinfo [n+];//
cout<<"请输入背包的最大容量:";
cin>>M;
cout<<endl;
int i;
for(i=;i<=n;i++)
{ goods[i].flag=i;
cout<<"请输入第"<<i<<"件物品的重量:";
cin>>goods[i].w;
cout<<"请输入第"<<i<<"件物品的效益:";
cin>>goods[i].p;
goods[i].p=goods[i].p/goods[i].w;//得出物品的效益,重量比
cout<<endl; } Insertionsort(goods,n);
bag(goods,M,n);
cout<<"press <1> to run agian"<<endl;
cout<<"press <0> to exit"<<endl;
cin>>j;
}
}

程序2

#include<stdio.h>
#include<stdlib.h>
#define Max 100/*定义栈结构*/
typedef struct list{ int data[Max]; int top;}
Seqstack; /*定义一个用来存储结果的链表*/
typedef struct List{ Seqstack result; struct List * Next;}
Seqlist,*Pointer;
void Inicial_List(Pointer p)
{ p=(Pointer)malloc(sizeof(Seqlist));
p->Next=NULL;}
Seqstack Push_Stack(int n,Seqstack s)
{ s.top++;
s.data[s.top]=n;
return s;}
int Add_Stack(Seqstack s)
{
Int total=,i;
if(s.top>=)
{ for(i=;i<=s.top;i++)
total+=s.data[i];
}
else
{ total=; }
return total;}
Seqstack Pop_Stack(Seqstack s)
{
printf("%d",s.data[s.top]);
if(s.top>=)
s.top--;
return s;}/*执行回溯操作的函数*//*参数说明:n->数的总的个数,a[]用来存放数的数组,k查找的总体积*/
Pointer Query_Result(int n,int b[],int k)
{ int i,j;
Seqstack mystack;
Seqlist *newnode;
Pointer r,p=NULL;
Inicial_List(p);
r=p; for(i=;i<n;i++)
{ mystack.top=-;
j=i;
while(j<n)
{
if(Add_Stack(mystack)+b[j]<k)
{ mystack=Push_Stack(b[j],mystack);
j++; }
else if(Add_Stack(mystack)+b[j]==k)
{ mystack=Push_Stack(b[j],mystack);
newnode=(Pointer)malloc(sizeof(Seqlist));
newnode->result=mystack;
newnode->Next=NULL;
r->Next=newnode;
r=newnode;
mystack=Pop_Stack(mystack);
j++;
}
else if(Add_Stack(mystack)+b[j]>k)
{
j++;
}
}
}
return p;
}
void Print_List(Pointer p)
{
int i,j=;
p=p->Next;
printf("welcome the outer\n");
if(p==NULL)
printf("there no results\n");
while(p!=NULL)
{
j++;
printf("the %d result is: ",j);
for(i=;i<=p->result.top;i++)
{ printf(" %d",p->result.data[i]);
}
p=p->Next;
printf("\n"); }
printf("\n");}
void Sort_Array(int b[],int n)
{
int i,j,temp;
for(i=;i<n;i++)
{ for(j=;j<n-i;j++)
{ if(b[j]<b[j+])
{ temp=b[j];
b[j]=b[j+];
b[j+]=temp;
}
}
}
}
void main()
{
int i,n,k,select,a[Max];
Pointer head;
printf("******************************************\n"); printf("1 start\n2 exit\n");
scanf("%d",&select);
while(select==)
{
printf("please input the total products\n");
scanf("%d",&n);
printf("please input the volumn of n products\n");
for(i=;i<n;i++)
{
printf("please input the %d integers",i+);
scanf("%d",&a[i]);
}
printf("\n");
printf("please input the volunm to put\n");
scanf("%d",&k);
Sort_Array(a,n);
head=Query_Result(n,a,k);
Print_List(head);
printf("******************************************\n");
printf("1 start\n2 exit\n"); scanf("%d",&select);
}
}

程序3

#include<stdio.h>
#include<stdlib.h>
#define Max 100
/*定义栈结构*/
typedef struct list{ int data[Max]; int top;}Seqstack;
/*定义一个用来存储结果的链表*/
typedef struct List
{
Seqstack result;
struct List * Next;
}
Seqlist,*Pointer;
void Inicial_List(Pointer p)
{
p=(Pointer)malloc(sizeof(Seqlist));
p->Next=NULL;
}
Seqstack Push_Stack(int n,Seqstack s)
{ s.top++;
s.data[s.top]=n;
return s;
}
int Add_Stack(Seqstack s)
{ int total=,i;
if(s.top>=)
{
for(i=;i<=s.top;i++)
total+=s.data[i];
}
else
{
total=;
}
return total;
}
Seqstack Pop_Stack(Seqstack s)
{
printf("%d",s.data[s.top]);
if(s.top>=)
s.top--; return s;}
/*执行回溯操作的函数*//*参数说明:n->数的总的个数,a[]用来存放数的数组,k查找的总体积*/
Pointer Query_Result(int n,int b[],int k)
{
int i,j;
Seqstack mystack;
Seqlist *newnode;
Pointer r,p=NULL;
Inicial_List(p);
r=p;
for(i=;i<n;i++)
{
mystack.top=-;
j=i;
while(j<n)
{
if(Add_Stack(mystack)+b[j]<k)
{
mystack=Push_Stack(b[j],mystack);
j++;
}
else if(Add_Stack(mystack)+b[j]==k)
{
mystack=Push_Stack(b[j],mystack);
newnode=(Pointer)malloc(sizeof(Seqlist));
newnode->result=mystack;
newnode->Next=NULL;
r->Next=newnode;
r=newnode;
mystack=Pop_Stack(mystack);
j++;
}
else if(Add_Stack(mystack)+b[j]>k)
{
j++;
}
}
}
return p;
}
void Print_List(Pointer p)
{
int i,j=;
p=p->Next;
printf("welcome the outer\n");
if(p==NULL)
printf("there no results\n");
while(p!=NULL)
{
j++;
printf("the %d result is: ",j);
for(i=;i<=p->result.top;i++)
{
printf(" %d",p->result.data[i]);
}
p=p->Next;
printf("\n");
}
printf("\n");
}
void Sort_Array(int b[],int n)
{
int i,j,temp;
for(i=;i<n;i++) { for(j=;j<n-i;j++)
{
if(b[j]<b[j+])
{
temp=b[j];
b[j]=b[j+];
b[j+]=temp;
}
}
}
}
void main()
{
int i,n,k,select,a[Max]; Pointer head;
printf("******************************************\n");
printf("1 start\n2 exit\n"); scanf("%d",&select);
while(select==)
{
printf("please input the total products\n");
scanf("%d",&n);
printf("please input the volumn of n products\n");
for(i=;i<n;i++)
{
printf("please input the %d integers",i+);
scanf("%d",&a[i]); } printf("\n");
printf("please input the volunm to put\n");
scanf("%d",&k); Sort_Array(a,n);
head=Query_Result(n,a,k);
Print_List(head);
printf("******************************************\n");
printf("1 start\n2 exit\n");
scanf("%d",&select);
}
}

贪心算法or背包问题的更多相关文章

  1. 贪心算法_01背包问题_Java实现

    原文地址:http://blog.csdn.net/ljmingcom304/article/details/50310789 本文出自:[梁敬明的博客] 1.贪心算法 什么是贪心算法?是指在对问题进 ...

  2. [C++] 贪心算法之活动安排、背包问题

    一.贪心算法的基本思想 在求解过程中,依据某种贪心标准,从问题的初始状态出发,直接去求每一步的最优解,通过若干次的贪心选择,最终得出整个问题的最优解. 从贪心算法的定义可以看出,贪心算法不是从整体上考 ...

  3. js贪心算法---背包问题

    /* * @param {Object} capacity 背包容量 6 * @param {Object} weights 物品重量 [2,3,4] * @param {Object} values ...

  4. 贪心算法(Greedy Algorithm)

    参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...

  5. 剑指Offer——贪心算法

    剑指Offer--贪心算法 一.基本概念 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的局部最优解.虽然贪心算法不能对 ...

  6. js算法初窥05(算法模式02-动态规划与贪心算法)

    在前面的文章中(js算法初窥02(排序算法02-归并.快速以及堆排)我们学习了如何用分治法来实现归并排序,那么动态规划跟分治法有点类似,但是分治法是把问题分解成互相独立的子问题,最后组合它们的结果,而 ...

  7. Java 算法(一)贪心算法

    Java 算法(一)贪心算法 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 一.贪心算法 什么是贪心算法?是指在对问题进行求 ...

  8. JavaScript算法模式——动态规划和贪心算法

    动态规划 动态规划(Dynamic Programming,DP)是一种将复杂问题分解成更小的子问题来解决的优化算法.下面有一些用动态规划来解决实际问题的算法: 最少硬币找零 给定一组硬币的面额,以及 ...

  9. python常用算法(6)——贪心算法,欧几里得算法

    1,贪心算法 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的的时在某种意义上的局部最优解. 贪心算法并不保证会得到最优解,但 ...

随机推荐

  1. 用openssl生成SSL使用的私钥和证书,并自己做CA签名(转)

    本 文记叙的是一次基于SSL的socket通讯程序开发中,有关证书,签名,身份验证相关的步骤. 我们的场景下,socket服务端是java语言编写的,客户端是c语言.使用了一个叫做matrixssl的 ...

  2. 关于android屏幕适配的问题(drawable-xxxxxxxx,dp,sp,px等等),偶尔看到了android源代码,关于dpi的区分的值

    上一篇博客说了一下.9.png图片http://blog.csdn.net/qq_23195583/article/details/46737419 当然,点九的是指的能够进行拉伸的.那么假设图片不能 ...

  3. 解决:Access denied for user ''@'sinochip-79e833' (using password: NO)

    uthentication to host '' for user '' using method 'mysql_native_password' failed with message: Acces ...

  4. 在asp.net中执行存储过程(转)

    摘自:http://www.cnblogs.com/smhy8187/articles/677742.html 声明:本例用的数据库是系统提供的pubs数据库,表是是employee,编程语言用C# ...

  5. WCF中可以使用SVCUtil.exe生成客户端代理类和配置文件

    1.找到如下地址“C:\Windows\System32\cmd.exe”  命令行工具,右键以管理员身份运行(视系统是否为win7 而定)         2.输入如下命令: C:\>cd C ...

  6. Python学习笔记011——内置函数eval()

    1 描述 eval()  函数用来执行一个字符串表达式,并返回表达式的值 2 语法 原文 eval(expression[, globals=None[, locals=None]]) express ...

  7. 【转】对 Go 语言的综合评价

    以前写过一些对 Go 语言的负面评价.现在看来,虽然那些评价大部分属实,然而却由于言辞激烈,没有点明具体问题,难以让某些人信服.在经过几个月实际使用 Go 来构造网站之后,我觉得现在是时候对它作一些更 ...

  8. Unix环境高级编程(二)文件和目录

    本章主要介绍的是文件结构及目录.重点是通过stat函数获取文件的结构信息,然后是文件目录及其遍历.学完本章后,编写了一个输出给的目录下的文件信息的程序. 首先是包含在<sys/stat.h> ...

  9. 计算机科学基础知识(一)The Memory Hierarchy

    一.前言 最近一个问题经常萦绕在我的脑海:一个学习电子工程的机械师如何称为优秀的程序员?(注:本文作者本科学习机械设计,研究生转到电子工程系学习,毕业后却选择了系统程序员这样的职业).经过思考,我认为 ...

  10. Shell中重定向&lt;&lt;EOF注意事项

    作者:iamlaosong 我们常常在shell脚本程序中用<<EOF重定向输入.将我们输入的命令字符串作为一个运行程序的输入,这样,我们就不须要在那个程序环境中手工输入命令,以便自己主动 ...