C language Solution hundred money buy hundred chicken problem _c language

Source: Internet
Author: User

The ancient Chinese mathematician Zhang Chujian the famous "hundred money buys hundred chickens" question in "The Book of Classics", the problem is described as follows: Chicken Weng one, worth five; chicken mother one, worth three; chickens three, valuable one, hundred money to buy hundred chickens, then Weng, mother, young each geometry?

The rooster a five yuan, the hen a three yuan, chicken three a dollar, now to use 100 dollars to buy 100 chickens, ask the rooster, hen, chicken how many?

Topic analysis

If you solve the problem of hundred money to buy hundred chickens in a mathematical way, you can abstract the problem into a formula group. Set the rooster x only, hen y only, chick z only, get the following equation group:
a:5x+3y+1/3z = 100
B:x+y+z = 100
c:0 <= x <= 100
d:0 <= y <= 100
e:0 <= z <= 100

If we solve this problem by solving the equation in a way that requires many guesses, one of the advantages of the computer is that the speed of calculation is particularly violent and no regrets, so we can bully her and ravage her! So we use the way to solve problems, we need to 101^3, but for computers, small case!

Code listings:

#include <stdio.h>
int main ()
{
  int i, j, K;
  printf ("Hundred yuan buy hundred chicken's problem all possible solutions are as follows: \ n");
  For (i=0 i <=, i++) for (
    j=0 J <=; j + +) for
      (k=0; k <=)
      {
        if (k++ && k%3==0 && i+j+k==100)
        {
          printf ("Rooster%2d only, hen%2d only, chick%2d only \ n", I, J, K);
        }
      return
  0;
}

Run Result:

Hundred yuan to buy hundred chicken problem all possible solutions are as follows:
Rooster 0 only, hen 25 only, Chick 75
Rooster 4, hen 18 only, chick 78
Rooster 8 only, hen 11 only, chick 81 Rooster 12, hen 4 only,
chick 8 4 only

Related Article

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.