1045 million questions: 1045 million questions, million questions

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.