A hundred chicken problem. Each rooster is 5 RMB, the hen is 3 RMB, every 3 chicks is 1 RMB, and 100 RMB is for 100 chickens.
Exhaustive method
Code 1
int main(){
int x,y,z;
cout<<"cock\t"<<"hen\t"<<"chick\t"<<endl;
for(x=0;x<20;x++){
for(y=0;y<33;y++){
z=100-x-y;
if(x*5+y*3+z/3.0==100){
cout<<x<<'\t'<<y<<'\t'<<z<<endl;
}
}
}
}
cock hen chick
0 25 75
4 18 78
8 11 81
12 4 84
Code 2
int main(){
int x,y,z;
cout<<"cock\t"<<"hen\t"<<"chick\t"<<endl;
for(x=0;x<20;x++){
for(y=0;y<33;y++){
z=100-x-y;
if(x*5+y*3+z/3==100){
cout<<x<<'\t'<<y<<'\t'<<z<<endl;
}
}
}
}
cock hen chick
0 25 75
3 20 77
4 18 78
7 13 80
8 11 81
11 6 83
12 4 84
Why does this happen?
Verify
int x=10;
int y = 3;
double a=10.0;
double b=3.0;
cout<<x/y<<endl;
cout<<a/b<<endl;
cout<<x/b<<endl;
3
3.33333
3.33333
Dividing two integers will round off the decimal.
So the difference between code 1 and code 2 3.0 and 3 is the case.
This is actually a difficult solution for every 3 chicks 1 ¥