C Language Hundred money white chicken problem

Source: Internet
Author: User

Problem Description: Chinese ancient mathematician Zhang Chujian in his "calculate the book" in his "hundred money White Chicken problem": A rooster worth five money, a hen worth three money, three chickens worth a money, now want to buy hundred chickens, how many cocks, hens, chickens?

Problem Analysis: We set the rooster, hen, chicken variables corresponding to cock, hen, chicken, then there must be a multiple of chicken three. At the same time satisfies the cock+hen+chicken=100, and satisfies the 5*cock+3*hen+chicken/3=100. We need to use circular structure to carry out the poor lifting. It takes a triple loop, a cycle to the cock, a loop to the hen, a chicken cycle. The value range of the cock for the range of 0~20,hen 0~33,chicken is 0~100 (but must be a multiple of 3).

1#include"stdio.h"2#include"conio.h"3 voidMain ()4 {5     intCock,hen,chicken;//variable definition6      for(cock=0; cock<= -; cock++)//The number of cocks is poor7          for(hen=0; hen<= -; hen++)//The number of hens is poor8              for(chicken=0; chicken<= -; chicken+=3)//The number of chickens is poor9             {Ten                 if(cock+hen+chicken== -&&cock*5+hen*3+chicken/3== -)//Conditional Judgment Oneprintf"cock=%2d,hen=%2d,chicken=%2d\n", Cock,hen,chicken);//Output A             } - getch (); -}

We can also use a two-loop approach, and for one of the variables we can take the representation of a different pair of variables. The following code:

#include"stdio.h"#include"conio.h"voidMain () {intCock,hen,chicken;//variable definition     for(cock=0; cock<= -; cock++)//The number of cocks is poor     for(chicken=0; chicken<= -; chicken+=3)//The number of chickens is poor{hen= --cock-chicken;//Number of Hens        if(cock+hen+chicken== -&&cock*5+hen*3+chicken/3== -&&hen>=0&&hen<= -)//Conditional Judgmentprintf"cock=%2d,hen=%2d,chicken=%2d\n", Cock,hen,chicken);//Output} getch ();}

Run fruit:

cock= 0,hen=25,chicken=75
cock= 4,hen=18,chicken=78
cock= 8,hen=11,chicken=81
cock=12,hen= 4,chicken=84

C Language Hundred money white chicken problem

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.