Problem Description: Chinese ancient mathematician Zhang Chujian in his "calculate the book" in his "hundred money White Chicken problem": A rooster worth five money, a hen worth three money, three chickens worth a money, now want to buy hundred chickens, how many cocks, hens, chickens?
Problem Analysis: We set the rooster, hen, chicken variables corresponding to cock, hen, chicken, then there must be a multiple of chicken three. At the same time satisfies the cock+hen+chicken=100, and satisfies the 5*cock+3*hen+chicken/3=100. We need to use circular structure to carry out the poor lifting. It takes a triple loop, a cycle to the cock, a loop to the hen, a chicken cycle. The value range of the cock for the range of 0~20,hen 0~33,chicken is 0~100 (but must be a multiple of 3).
1#include"stdio.h"2#include"conio.h"3 voidMain ()4 {5 intCock,hen,chicken;//variable definition6 for(cock=0; cock<= -; cock++)//The number of cocks is poor7 for(hen=0; hen<= -; hen++)//The number of hens is poor8 for(chicken=0; chicken<= -; chicken+=3)//The number of chickens is poor9 {Ten if(cock+hen+chicken== -&&cock*5+hen*3+chicken/3== -)//Conditional Judgment Oneprintf"cock=%2d,hen=%2d,chicken=%2d\n", Cock,hen,chicken);//Output A } - getch (); -}
We can also use a two-loop approach, and for one of the variables we can take the representation of a different pair of variables. The following code:
#include"stdio.h"#include"conio.h"voidMain () {intCock,hen,chicken;//variable definition for(cock=0; cock<= -; cock++)//The number of cocks is poor for(chicken=0; chicken<= -; chicken+=3)//The number of chickens is poor{hen= --cock-chicken;//Number of Hens if(cock+hen+chicken== -&&cock*5+hen*3+chicken/3== -&&hen>=0&&hen<= -)//Conditional Judgmentprintf"cock=%2d,hen=%2d,chicken=%2d\n", Cock,hen,chicken);//Output} getch ();}
Run fruit:
cock= 0,hen=25,chicken=75
cock= 4,hen=18,chicken=78
cock= 8,hen=11,chicken=81
cock=12,hen= 4,chicken=84
C Language Hundred money white chicken problem