1045 million questions: 1045 million questions, million questions
-
Description:
-
Use less than or equal to n yuan to buy 100 chickens, 5 yuan for chicken, 3 yuan for chicken, and one type of chicken with 1/3 yuan each, respectively, as x, y only, z only. Programming all possible solutions for x, y, and z.
-
Input:
-
There are multiple groups of test data, and n is input.
-
Output:
-
For each input group, output all feasible solutions x, y, and z in the order of increasing x, y, and 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 is as follows. A triplicate loop was used at the beginning, and then a slight optimization was made to the dual-loop:
-
Code for triple loop:
-
#include <stdio.h>int main(){ int n; int x,y,z; while(scanf("%d",&n) != EOF){ x = 0; y = 0; z = 0; int money1 = n; for(int i = 0; i <= money1/5; i++){ x = i; int money2 = money1 - 5 * i; for(int j = 0; j <= money2/3; j++){ y = j; int money3 = money2 - 3 * j; z = 100 - x - y; if(z >= 0 && z <= 3 * money3){ printf("x=%d,y=%d,z=%d\n"); } } } } return 0;}
-
Two-loop code:
-
#include <stdio.h>int main(){ int n; int x,y,z; while(scanf("%d",&n) != EOF){ x = 0; y = 0; z = 0; int money1 = n; for(int i = 0; i <= money1/5; i++){ x = i; int money2 = money1 - 5 * i; for(int j = 0; j <= money2/3; j++){ y = j; int money3 = money2 - 3 * j; for(int k = 0; k <= money3 * 3; k++){ z = k; if(x+y+z == 100){ printf("x=%d,y=%d,z=%d\n",x,y,z); } } } } } return 0;}