(JAVA实现)设有各不同的面值的人命币若干,编写一个计算机程序,对任意输入的一个金额,给出能够组合出这个值的最佳可能,要求使用的币值个数最少。
使用基本运算除/,和求余操作%,进行操作得到所需币值个数。
mport java.util.Scanner;
public class Treedian15 {
public static void compute(double x){
int i,j,k,l,c,v,q,w,e,r,t,y;
i = (int) (x / 100);
x = x % 100;
if(i>0){System.out.println("100元 "+i+"个");}
j= (int) (x/50);
x=x%50;
if(j>0)System.out.println("50元 "+j+"个");
k= (int) (x/20);
x=x%20;
if(k>0)System.out.println("20元 "+k+"个");
l= (int) (x/10);
x=x%10;
if(l>0)System.out.println("10元 "+l+"个");
c= (int) (x/5);
x=x%5;
if(c>0)System.out.println("5元 "+c+"个");
v= (int) (x/2);
x=x%2;
if(v>0)System.out.println("2元 "+v+"个");
q= (int) (x/1);
x=x%1;
if(q>0)System.out.println("1元 "+q+"个");
w= (int) (x/0.5);
x=x%0.5;
if(w>0)System.out.println("5角 "+w+"个");
e= (int) (x/0.2);
x=x%0.2;
if(e>0)System.out.println("2角 "+e+"个");
r= (int) (x/0.1);
x=x%0.1;
if(r>0)System.out.println("1角 "+r+"个");
t= (int) (x/0.05);
x=x%0.05;
if(t>0)System.out.println("5分 "+t+"个");
y= (int) (x/0.01);
x=x%0.01;
if(y>0)System.out.println("1分 "+y+"个");
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("请输入金额:");
double x=scanner.nextDouble();
compute(x);
}
}