hundred dollars to buy hundred chickens

Source: Internet
Author: User

Chinese ancient mathematician Zhang Chujian in "The Book of calculation" of the mathematical Problem: the chicken Weng A value of five, chicken mother a valuable three, chickens three valuable one. Hundred money buys hundred chicken, asks the chicken Weng, the chicken mother, chickens each geometry?
How is the go language implemented?

Method One
package mainimport "fmt"func main() {    var n int    // i 为鸡翁、j为鸡母、k为鸡雏    for i := 0; i <= 100; i++ {        for j := 0; j <= 100; j++ {            for k := 0; k <= 100; k++ {                // 鸡翁的价格                icost := i * 5                // 鸡母的价格                jcost := j * 3                // 鸡雏的价格                kcost := k / 3                // 价格总和                sum := icost + jcost + kcost                // 购买的数量总和                count := i + j + k                // 小鸡的数量必须为3的整倍数。不然会出现小数                if sum == 100 && count == 100 && k % 3 == 0 {                    n++                    fmt.Printf("第%d种方法---鸡翁数量: %d,鸡母数量: %d,鸡雏数量: %d\n",n,i,j,k)                }            }        }    }}
Method Two
package mainimport "fmt"func main() {    var n int    // i 为鸡翁、j为鸡母、k为鸡雏    for i := 0; i <= 100; i++ {        for j := 0; j <= 100; j++ {            k := 100 - i - j            if k % 3 == 0 {                sum := i*5 + j*3 + k/3                if sum == 100 {                    n++                    fmt.Printf("第%d种方法---鸡翁数量: %d,鸡母数量: %d,鸡雏数量: %d\n",n,i,j,k)                }            }        }    }}
Output results
第1种方法---鸡翁数量: 0,鸡母数量: 25,鸡雏数量: 75第2种方法---鸡翁数量: 4,鸡母数量: 18,鸡雏数量: 78第3种方法---鸡翁数量: 8,鸡母数量: 11,鸡雏数量: 81第4种方法---鸡翁数量: 12,鸡母数量: 4,鸡雏数量: 84

hundred dollars to buy hundred chickens

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.