Java 代码

        
public class an {
    public static void main(String[] args) {
        int count = 0;
        for (int gj = 0; gj <= 100; gj++) {
            for (int mj = 0; mj <= 100; mj++) {
                for (int xj = 0; xj <= 100; xj++) {
                    boolean condition1 = (gj + mj + xj == 100);
                    boolean condition2 = (5 * gj + 3 * mj + xj / 3 == 100);
                    boolean condition3 = (xj % 3 == 0);
                    count++;
                    if (condition1 && condition2 && condition3) {
                        System.out.println("公鸡:" + gj + " 母鸡:" + mj + " 小鸡:" + xj);
                    }
                }
            }
        }
        System.out.println("算法1的 if 执行次数:" + count);
    }
}
        
    

运行结果

        
公鸡:0 母鸡:25 小鸡:75
公鸡:4 母鸡:18 小鸡:78
公鸡:8 母鸡:11 小鸡:81
公鸡:12 母鸡:4 小鸡:84
算法1的 if 执行次数:1030301
        
    
返回主页