JAVA编程,玫瑰2元一支,百合3元一支,菊花5元一支,康乃馨6元一支,20元正好花完没种各多少支

JAVA编程,用编程语言写出代码,输出所有可行方案,谢谢
2025-04-04 19:16:40
推荐回答(1个)
回答1:

public static void main(String[] args) {
        for(int m = 0;m<=10;m++) {//玫瑰,全买,对多买10只
            for(int b = 0;b<=7;b++) {//百合,全买,对多买7只
                for(int j = 0;j<=4;j++) {//菊花全买,对多买4只
                    for(int k = 0;k<=4;k++) {//康乃馨全买,对多买4只
                        int total = 2*m+3*b+5*j+6*k;
                        if(20==total) {
                            System.out.println("玫瑰买"+m+"支,百合买"+b+"支,菊花买"+j+"支,康乃馨买"+k+"支");
                        }
                    }
                }
            }
        }
    }