其实很简单, 输入总钱数, 然后用for循环嵌套, 零钱面额有几种, 就嵌套几层, 同时每种零钱的张数小于等于总钱数/面额 .
对于有几种方案的话 , 加一个计数器就可以了
import java.util.Scanner;
public class Main{
public static void main(String [] args){
Scanner in = new Scanner(System.in);
int amount = in.nextInt();
int count=0;
OUT:
for(int one = 0; one <=amount; one++){
for(int five = 0; five <=amount/5; five++){
for(int ten = 0; ten <=amount/10; ten++){
for(int twenty = 0; twenty <=amount/20; twenty++){
if(one+five*5+ten*10+twenty*20 == amount){
count++;
System.out.println(one+"张一元\n"+
five+"张五元\n"+ten+"张十元\n"+twenty+"张二十元\n");
//break OUT;
}
}
}
}
}
System.out.println("一共计算了"+count+"种方法!");
}
}