用户输入一个金额,计算机用小金额拼凑成指定金额
package test01;
import java.util.Scanner;
/*
* 凑硬币
*/
public class Coucoin {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("请输入金额:");
int amount = in.nextInt();
//标记的使用
Coin:
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)
{
System.out.println(one+"张一元,"+five+"张五元,"+ten+"张十元,"+twenty+"张二十元的总和是"+amount);
break Coin;
}
}
}
}
}
}
}
效果如下图所示: