The ancient Chinese mathematician Zhang Chujian the famous "hundred money buys hundred chickens" question in "The Book of Classics", the problem is described as follows: Chicken Weng one, worth five; chicken mother one, worth three; chickens three, valuable one, hundred money to buy hundred chickens, then Weng, mother, young each geometry?
The rooster a five yuan, the hen a three yuan, chicken three a dollar, now to use 100 dollars to buy 100 chickens, ask the rooster, hen, chicken how many?
Topic analysis
If you solve the problem of hundred money to buy hundred chickens in a mathematical way, you can abstract the problem into a formula group. Set the rooster x only, hen y only, chick z only, get the following equation group:
a:5x+3y+1/3z = 100
B:x+y+z = 100
c:0 <= x <= 100
d:0 <= y <= 100
e:0 <= z <= 100
If we solve this problem by solving the equation in a way that requires many guesses, one of the advantages of the computer is that the speed of calculation is particularly violent and no regrets, so we can bully her and ravage her! So we use the way to solve problems, we need to 101^3, but for computers, small case!
Code listings:
#include <stdio.h>
int main ()
{
int i, j, K;
printf ("Hundred yuan buy hundred chicken's problem all possible solutions are as follows: \ n");
For (i=0 i <=, i++) for (
j=0 J <=; j + +) for
(k=0; k <=)
{
if (k++ && k%3==0 && i+j+k==100)
{
printf ("Rooster%2d only, hen%2d only, chick%2d only \ n", I, J, K);
}
return
0;
}
Run Result:
Hundred yuan to buy hundred chicken problem all possible solutions are as follows:
Rooster 0 only, hen 25 only, Chick 75
Rooster 4, hen 18 only, chick 78
Rooster 8 only, hen 11 only, chick 81 Rooster 12, hen 4 only,
chick 8 4 only