001、百钱买百鸡

公鸡5钱一只,母鸡3钱一只,小鸡1钱三只;

要用一百钱要买100只鸡,问公鸡、母鸡、小鸡个多少只?

由于只有100文钱,则5x<100 => 0<x<20, 同理  0<y<33,那么z=100-x-y

第一种方法:

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int x,y,z;
 6 
 7     for(x = 0;x < 20;x++)
 8     {
 9         for(y = 0;y < 33;y++)
10         {
11             z = 100-x-y;
12             if((5*x+3*y+z/3 == 100)&&(z%3 == 0))
13                 printf("x = %d  y = %d  z = %d
",x,y,z);
14         }
15     }
16     return 0;
17 
18 }
View Code

第二种方法:

由 (2)x3-(1)  --->  y = 25-7x/4;   z = 75+3x/4;

令 t = x/4;---> x = 4t;   y = 25-7t;  z = 75+3t;

要保证0<x,y,z<100的话,那么k <= 3;

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int x,y,z,t;
 6     for(t = 0;t <= 3;t++)
 7     {
 8         x = 4*t;
 9         y = 25-7*t;
10         z = 75+3*t;
11         printf("x = %d    y = %d    z = %d
",x,y,z);
12     }
13     return 0;
14 }
View Code
原文地址:https://www.cnblogs.com/yang-cheng/p/12894118.html