母雞每隻5元,公雞每隻3元,小雞1元3只。 現用100元買100只雞,求出全部可能的解。ios
算法分析:
設母雞、公雞、小雞分別爲x、y、z只,算法
1. 需知足2個條件: x+y+z=100和spa
5x+3y+z/3=100blog
2. 對於此類實際問題,考慮x,y,z的取值範圍:
0<= x <=100, io
0<= y <=100, stream
0<= z <=100 im
#include <iostream>
using namespace std;
int main( )
{ int x,y,z;
for (x=0; x<=100;x++)
for (y=0; y<=100; y++)
for (z=0; z<=100; z++)
if ( x+y+z==100 && 5*x+3*y+z/3==100 )
cout<<x<<' '<<y<<' '<<z<<endl;
system("pause"); return 1;
}db
*********************************************************************img
#include <iostream>
using namespace std;
int main( )
{ int x,y,z;
for (x = 0; x <= 14; x++)
{ y = 100 -7 * x;
if (y % 4) continue; else y /= 4;
z = 100 -x -y;
if (z % 3) continue;
cout<<x<<' '<<y<<' '<<z<<endl;
}
system("pause"); return 1;
}co