题目要求
编写程序,读取用户输入的代表总金额的double值,打印表示该金额所需的最少纸币张数和硬币个数,打印从最大金额开始。纸币的种类有十元、五元、一元,硬币的种类有五角、一角、贰分、壹分。
输入格式:
47.63
输出格式:
4 张十元
1 张五元
2 张一元
1 个五角
1 个一角
1 个贰分
1 个壹分
输入样例:
47.63
输出样例:
4 张十元
1 张五元
2 张一元
1 个五角
1 个一角
1 个贰分
1 个壹分
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
double a=input.nextDouble();
int b=(int)a/10; //十元
int c=((int)a%10)/5; //五元
int d=0; //一元
if(c==0){
d=((int)a%10);
}
else {
d=((int)a%10)-5;
}
int e=(int)(a*100-b*1000-c*500-d*100);
int f=(int)e/50; //五角
int j=((int)e%50)/10; //一角
int h=((int)e%50)%10/2; //贰分
int i= (int) (e-f*50-j*10-2*h);//壹分
System.out.println(b+" 张十元");
System.out.println(c+" 张五元");
System.out.println(d+" 张一元");
System.out.println(f+" 个五角");
System.out.println(j+" 个一角");
System.out.println(h+" 个贰分");
System.out.print(i+" 个壹分");;
}
}
这个代码错在那,提交结果部分正确
兄弟你这个还没搞定啊,前面我不是给你写了一个么,试试这个ok不
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
long aLong = Math.round((a * 100));
long b = aLong / 1000;
long c = aLong % 1000 / 500;
long d = aLong % 500 / 100;
long f = aLong % 100 / 50;
long j = aLong % 50 / 10;
long h = aLong % 10 / 2;
long i = aLong % 2;
System.out.println(b + " 张十元");
System.out.println(c + " 张五元");
System.out.println(d + " 张一元");
System.out.println(f + " 个五角");
System.out.println(j + " 个一角");
System.out.println(h + " 个贰分");
System.out.print(i + " 个壹分");