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