One hundred money white chicken 15

One hundred white chicken money

demand analysis

Ancient Chinese mathematician Zhang Qiu Jian in mathematical problem "was considered" a book made of: a chicken Weng worth five chickens mother a valuable three chicks three valuable. One hundred to buy one hundred chicken, chicken and asked Weng, mother chicken, chicks each geometry?
(Chicken Weng a 5, a female chicken 3, a three chicks, will buy a lot of combinations can be 100, the final number must also be 100 chickens!)

Step analysis

Step 1: Analysis of how many chickens Weng can buy range: 0 <= I <= 20 is;
2. Step: Analysis of the range of how many chickens master may buy: 0 <= J <= 33 is;
. 3. the third step: quantitative analysis chicks 100 - i - j. (Total number of chickens to buy must be just 100. And the total amount requested must be 100 yuan).
4. Fourth Step: Analysis chicks must be a multiple of 3 (three per buy 3), then the total amount must be exactly three membered assembly 100 can output!
Note: The number of chicks just must be a multiple of 3! , Chicks should only buy three three

public class ExecDemo {
    public static void main(String[] args) {
        // a.使用一个循环来选择鸡翁买的只数
        for(int i = 0 ; i <= 20 ; i++ ){
            // b.使用一个循环选择鸡母的只数
            for(int j = 0 ; j <= 33 ; j++ ){
                // c.得到鸡雏的只数k
                int k = 100 - i - j ;
                // d.判断三种类型的鸡的总金额是否刚刚好是100元 ,是这种组合的结果就满足了百钱百鸡
                // 注意:鸡雏的数量必须刚刚是3的倍数!
                if(k % 3 == 0 && i * 5 + j * 3 + k/3 == 100){
                    System.out.println("鸡翁:"+i+",鸡母:"+j +",鸡雏:"+k);
                }
            }
        }
    }
}

Published 34 original articles · won praise 16 · views 290

Guess you like

Origin blog.csdn.net/qq_41005604/article/details/105180967