编写程序实现用一元人民币换成一分,两分,五分的硬币共五十枚(用到循环)

java

循环

用一元人民币换成一分,两分,五分的硬币共五十枚

public class Test{
    public static void main(String []args) {
        int i, j, k;
        for (i = 0; i < 20; i++) {//五分
            for (j = 0; j < 50 - i; j++) {//两分
                k = 50 - i - j;//一分
                if (k + 2 * j + 5 * i == 100) {
                    System.out.print("一分硬币" + k + "枚,");
                    System.out.print("两分硬币" + j + "枚,");
                    System.out.println("五分硬币" + i + "枚");
                }
            }
        }
    }
}

        int one = 1;
        int two = 2;
        int five = 5;
        for (int a = 1; a < 100; a++){
            for (int b = 1; b < 50; b++){
                for (int c = 1; c < 20; c++){
                    int i = a * one + b * two + c * five;
                    if (i == 100){
                        System.out.println(a + "个" + one + "分 + " + b + "个" + two + "分 + " + c + "个" + five + "分 = 100分 = 1元");
                    }
                }
            }
        }