-
Title Description:
-
Use less than equals n yuan to buy 100 chickens, big chicken 5 yuan/Only, chicken 3 yuan/only, and 1/3 yuan each one of a chicken, respectively recorded as x, y only, z only. Programming solves all possible solutions for x, Y, Z.
-
Input:
-
There are several groups of test data, input n.
-
Output:
-
For each set of inputs, output all possible solutions in X, Y, z, and in order of X, Y, Z.
-
Sample input:
-
40
-
-
Sample output:
-
-
x=0,y=0,z=100x=0,y=1,z=99x=0,y=2,z=98x=1,y=0,z=99
-
-
-
-
The code below, started with a triple loop, and then slightly optimized, changed to a dual loop:
-
-
The triple Loop code:
-
#include <stdio.h>int main () { int n; int x,y,z; while (scanf ("%d", &n)! = E of) { x = 0; y = 0; z = 0; &N Bsp int money1 = n; for (int i = 0; I <= money1/5; i++) { x = i; int money2 = money1-5 * i; &NB Sp for (int j = 0; J <= Money2/3; j + +) { y = j; & nbsp int Money3 = money2-3 * j; z = 100- x-y; if (z >= 0 && z <= 3 * money3) { &N Bsp printf ("x=%d,y=%d,z=%d\n"); &NBS P   } } } } return 0;}
-
-
Dual loop code:
-
#include <stdio.h>int main () { int n; int x,y,z; while (scanf ("%d", &N)! = EOF) { x = 0; y = 0; z = 0;&nbs P int money1 = n; for (int i = 0; I <= money1/5; i++) { x = i; int money2 = money1-5 * i; &NB Sp for (int j = 0; J <= Money2/3; j + +) { y = j; & nbsp int Money3 = money2-3 * j; &NB Sp for (int k = 0; k <= Money3 * 3; k++) { z = k; if (x+y+z = =) { &NBSP; printf ("x=%d,y=%d,z=%d\n", X, Y, z); &NBSP ; } } } } } return 0;}
Nine degree topic 1045: Hundred Chicken problem