贪心算法or背包问题

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

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

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

程序1

  1 #include <iostream.h>
  2 struct goodinfo
  3 {
  4  float p; //物品效益
  5  float w; //物品重量
  6  float X; //物品该放的数量
  7  int flag; //物品编号
  8 };//物品信息结构体
  9 
 10 void Insertionsort(goodinfo goods[],int n)
 11 {
 12  int j,i;
 13  for(j=2;j<=n;j++)
 14  {
 15   goods[0]=goods[j];
 16      i=j-1;
 17   
 18   while (goods[0].p>goods[i].p)
 19   {
 20    goods[i+1]=goods[i];
 21    i--;
 22   }
 23   goods[i+1]=goods[0];
 24  }
 25 }//按物品效益,重量比值做升序排列
 26 
 27 void bag(goodinfo goods[],float M,int n)
 28 { 
 29  
 30  float cu;
 31  int i,j;
 32  for(i=1;i<=n;i++)
 33   goods[i].X=0;
 34  cu=M;  //背包剩余容量
 35  for(i=1;i<n;i++)
 36  {
 37   if(goods[i].w>cu)//当该物品重量大与剩余容量跳出
 38    break;
 39   
 40    goods[i].X=1;
 41    cu=cu-goods[i].w;//确定背包新的剩余容量
 42  }
 43  if(i<=n)
 44   goods[i].X=cu/goods[i].w;//该物品所要放的量
 45 /*按物品编号做降序排列*/ 
 46  for(j=2;j<=n;j++)
 47  {
 48   goods[0]=goods[j];
 49      i=j-1;
 50   
 51   while (goods[0].flag<goods[i].flag)
 52   {
 53    goods[i+1]=goods[i];
 54    i--;
 55   }
 56   goods[i+1]=goods[0];
 57  }
 58 ///////////////////////////////////////////
 59  cout<<"最优解为:"<<endl;
 60  for(i=1;i<=n;i++)
 61  {
 62   cout<<""<<i<<"件物品要放:";
 63   cout<<goods[i].X<<endl;
 64  }
 65 }
 66 
 67 void main()
 68 { 
 69  cout<<"|--------运用贪心法解背包问题---------|"<<endl;
 70  cout<<"|---power by zhanjiantao(028054115)---|"<<endl;
 71  cout<<"|-------------------------------------|"<<endl;
 72  int j;
 73  int n;
 74  float M;
 75  goodinfo *goods;//定义一个指针
 76  while(j)
 77  {
 78  cout<<"请输入物品的总数量:";
 79  cin>>n;
 80  goods=new struct goodinfo [n+1];//
 81  cout<<"请输入背包的最大容量:";
 82  cin>>M;
 83  cout<<endl;
 84  int i;
 85  for(i=1;i<=n;i++)
 86   { goods[i].flag=i;
 87    cout<<"请输入第"<<i<<"件物品的重量:";
 88    cin>>goods[i].w;
 89    cout<<"请输入第"<<i<<"件物品的效益:";
 90    cin>>goods[i].p;
 91    goods[i].p=goods[i].p/goods[i].w;//得出物品的效益,重量比
 92    cout<<endl;
 93    
 94   }
 95  
 96  Insertionsort(goods,n);
 97  bag(goods,M,n);
 98  cout<<"press <1> to run agian"<<endl;
 99  cout<<"press <0> to exit"<<endl;
100  cin>>j;
101  }
102 }

程序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=0,i;   
if(s.top>=0)  
 {   for(i=0;i<=s.top;i++) 
total+=s.data[i];  
 }   
else   
{ total=0;   }   
return total;}
Seqstack Pop_Stack(Seqstack s)
{
  printf("%d",s.data[s.top]);
 if(s.top>=0) 
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=0;i<n;i++) 
{  mystack.top=-1;  
 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=0;
 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=0;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=0;i<n;i++)
 {  for(j=0;j<n-i;j++)  
{      if(b[j]<b[j+1])    
  {   temp=b[j]; 
  b[j]=b[j+1]; 
  b[j+1]=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==1) 
 {   
printf("please input the total products\n");  
 scanf("%d",&n);  
 printf("please input the volumn of n products\n");
   for(i=0;i<n;i++)  
 {   
 printf("please input the %d integers",i+1);   
 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);
  }
 }
View Code

程序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=0,i;  
 if(s.top>=0) 
  {   
for(i=0;i<=s.top;i++)
 total+=s.data[i]; 
  }  
 else   
{
 total=0;  
 }   
return total;
}
Seqstack Pop_Stack(Seqstack s)
{
  printf("%d",s.data[s.top]); 
if(s.top>=0) 
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=0;i<n;i++)
 {  
mystack.top=-1; 
  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=0; 
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=0;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=0;i<n;i++) {  for(j=0;j<n-i;j++)  
{     
 if(b[j]<b[j+1])      
{ 
  temp=b[j];  
 b[j]=b[j+1];  
 b[j+1]=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==1)  
{ 
  printf("please input the total products\n"); 
  scanf("%d",&n); 
  printf("please input the volumn of n products\n");  
 for(i=0;i<n;i++)  
 {  
  printf("please input the %d integers",i+1);   
 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); 
 } 
}
View Code

 

posted @ 2013-06-21 20:43  夜雨阑珊  阅读(490)  评论(0编辑  收藏  举报