Question: One chicken is worth five; one mother is worth three; three chicks are worth one. Hundreds of money to buy a hundred chickens, ask the chicken weng, the mother chicken, and the chicks?
Note: Buying 100 chickens with 100 dollars means that you just spend 100 dollars and you need to buy 100 chickens.
#include <stdio.h> main() { int x,y,z; for(x=1;x<=20;x++) for(y=1;y<=33;y++) for(z=3;z<=99;z+=3) { if((5*x+3*y+z/3==100)&&(x+y+z==100))/*Whether the conditions of Baiqian and Baiji are satisfied*/ printf("cock=%d ,hen=%d,chicken=%d\n",x,y,z); } }
This kind of indefinite equation problem, the general solution:
Set the variable variable as a function, and then use the nested for loop to write out the value range of each variable.
Then in a nested for loop write out the relationship that the function should satisfy
Finally, use the printf statement to output each variable.