-
Topic Description Narration:
-
Use less than equals n yuan to buy 100 chickens only. Big Chicken 5 yuan/Just. Chick 3 yuan/Just, and 1/3 yuan every single chick, recorded as x only, y just, Z just. Programming solves all possible solutions for x, Y, Z.
-
Input:
-
Test data has multiple groups, input n.
-
Output:
-
For each set of inputs, output all feasible solutions in X, Y, z, and X, Y, Z, sequentially.
-
Example input:
-
40
-
-
Example output:
-
-
x=0,y=0,z=100x=0,y=1,z=99x=0,y=2,z=98x=1,y=0,z=99
-
-
-
-
Code such as the following. Started with a triple loop. It was slightly optimized and changed into a double 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;}
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Nine degree title 1045: Hundred Chicken problem