【百鸡问题--穷举法】算法实现

 

  1 /*******************************************************************
  2 *
  3 *问题描述:百鸡问题(古代数学家张丘建<<算经>>提出)
  4 *
  5 *公鸡每只5元,母鸡每只3元,小鸡3只1元
  6 *用100元钱买100只鸡,求公鸡、母鸡、小鸡只数
  7 *--穷举法
  8 *
  9 ********************************************************************/
 10 #include<iostream.h> 
 11 //#include<malloc.h>
 12 
 13 int sum=100;//所购买三种鸡的总数
 14  
 15 
 16 struct distribute
 17 {
 18     int cock_num;//公鸡数
 19     int hen_num;//母鸡数
 20     int chick_num;//小鸡数 
 21     distribute *pNext;
 22     
 23     distribute operator=(distribute c)//重载运算符
 24     { 
 25         cock_num=c.cock_num;
 26         hen_num=c.hen_num;
 27         chick_num=c.chick_num;
 28     }
 29 };
 30 
 31 distribute* Problem_Of_A_Hundred_Chickens(int &number);
 32 distribute* Problem_Of_A_Hundred_Chickens(int &number,int flag);//改进的算法
 33 void print(distribute pDis[],int n);
 34 
 35 void main()
 36 
 37     int num;
 38     distribute* p=Problem_Of_A_Hundred_Chickens(num,1);
 39     print(p,num); 
 40     //print(Problem_Of_A_Hundred_Chickens(num),num);
 41     
 42 }
 43 
 44 distribute* Problem_Of_A_Hundred_Chickens(int &number)
 45 {
 46     distribute* first=NULL;
 47     distribute *p1=new distribute;
 48     distribute *p2;
 49     number=0;
 50     int i,j,k;
 51     for(i=0;i<sum;i++)
 52     {
 53         for(j=0;j<sum;j++)
 54         {
 55             for(k=0;k<sum;k++)
 56             {
 57                 if((i+j+k)==sum&&(5*i+3*j+k/3)==100&&(k%3==0))
 58                 {
 59                     number++;
 60                     //pDis=(struct distribute*)malloc(sizeof(struct distribute));    
 61                     p1->cock_num=i;
 62                     p1->hen_num=j;
 63                     p1->chick_num=k;
 64                     if(number==1)
 65                     {
 66                         first=p1;
 67                     }
 68                     p2=p1;
 69                     p1=new distribute;
 70                     p2->pNext=p1;                    
 71                 }
 72             }
 73         }
 74     }
 75     p2->pNext=NULL;
 76     //print(first,number); 
 77     return first;
 78 }
 79 //------------------------------------------------------------//
 80 //改进算法
 81 //------------------------------------------------------------//
 82 distribute* Problem_Of_A_Hundred_Chickens(int &number,int flag)
 83 {
 84     distribute* first=NULL;
 85     distribute *p1=new distribute;
 86     distribute *p2;
 87     number=0;
 88     int i,j,k;
 89     for(i=0;i<=100/5;i++)
 90     {
 91         for(j=0;j<=100/3;j++)
 92         {
 93             k=sum-i-j;
 94             if(k%3==0&&(5*i+3*j+k/3)==100)
 95             {
 96                     number++;
 97                     p1->cock_num=i;
 98                     p1->hen_num=j;
 99                     p1->chick_num=k;
100                     if(number==1)
101                     {
102                         first=p1;
103                     }
104                     p2=p1;
105                     p1=new distribute;
106                     p2->pNext=p1;    
107             }
108         }
109     }
110     p2->pNext=NULL;
111     return first;
112 }
113 void print(distribute *pDis,int n)
114 {
115     cout<<"公鸡数"<<"  "<<"母鸡数"<<"  "<<"小鸡数"<<"\n";
116  
117     for(int i=0;i<n;i++)
118     {
119         cout<<"   "<<pDis->cock_num<<"      "<<pDis->hen_num<<"      "<<pDis->chick_num;
120         cout<<endl;
121         pDis=pDis->pNext;
122     }
123 }
124  

 

 

运行结果:

posted on 2011-03-29 01:05  COS  阅读(994)  评论(0编辑  收藏  举报