12-RMB for chicken (algorithm)

Source: Internet
Author: User

Problem

In his computation Sutra, Zhang qiujian, a Chinese ancient mathematician, proposed the famous question of "buying a hundred chickens for a hundred dollars": Wong Yi, worth five yuan, and mother of a rooster, valuable 3, chicken 3, valuable 1, yuan to buy a hundred chickens, Q Weng, Mother, and chicks ry?

If it is a column equation:

Set rooster X, hen y, and chicken to 100-x-y.

There are 5x + 3y + (100-x-y)/3 = 100

Resolved to 7X + 4y = 100

Because X can be 20 at the maximum,

According to the equation, X must be an even number, so it may be, 20.

Then, we bring the equation calculation to obtain the values of Y: 25, 11, 18, and 4.


How can I use a program?

/*** Buy a chicken for hundreds of dollars ** @ author supermanxkq **/public class test10 {public static void main (string [] ARGs) {for (float gongji = 0; gongji <= 20; gongji ++) {for (float Muji = 0; Muji <= 33; Muji ++) {for (float Xiaoji = 0; Xiaoji <= 100; xiaoji ++) {If (gongji + Muji + Xiaoji = 100 & gongji * 5 + Muji * 3 + Xiaoji/3 = 100) {system. out. println ("Rooster:" + gongji + "Hen:" + Muji + "chicken:" + Xiaoji );}}}}}}

Because the number of chickens is defined as an integer, the number of chicks may not be an integer, so the number of chickens is defined as a floating point to ensure that the total amount of money spent is an integer of 100.

However, there are other methods. See four methods.



For example, if you want to buy a chicken for a hundred dollars, you can calculate the synthesis of 10 yuan for 1 yuan, 2 yuan, and 5 tuples.

Public class test11 {public static void main (string [] ARGs) {system. out. println ("all methods that make up 10 RMB output:"); int method = 0; For (INT I = 0; I <= 10; I ++) {for (Int J = 0; j <= 5; j ++) {for (int K = 0; k <= 2; k ++) {if (I * 1 + J * 2 + K * 5 = 10) {system. out. println ("1 yuan:" + I + "2 yuan:" + J + "5 yuan:" + k); Method ++ ;}}} system. out. println ("Total" + method + "methods! ");}}

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.