package a_od_test;
import java.util.Scanner;
/*
货币单位换算
题目描述
记账本上记录了若干条多国货币金额,需要转换成人民币分(fen),汇总后输出。
每行记录一条金额,金额带有货币单位,格式为数字+单位,可能是单独元,或者单独分,或者元与分的组合。
要求将这些货币全部换算成人民币分(fen)后进行汇总,汇总结果仅保留整数,小数部分舍弃。
元和分的换算关系都是1:100
1CNY=100fen(1元=100分)
1HKD=100cents(1港元=100港分)
1JPY=100sen(1日元=100仙)
1EUR=100eurocents(1欧元=100欧分)
1GBP=100pence(1英磅=100便士)
汇率如下:
100CNY=1825JPY=123HKD=14EUR=12GBP
示例1:
输入:
1
100CNY
输出:
10000
示例2:
输入:
1
3000fen
输出:
3000
示例3:
输入:
1
123HKD
输出:
10000
示例4:
输入:
2
20CNY53fen
53HKD87cents
输出:
6432
*/
public class Main10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = Integer.parseInt(sc.nextLine());
String digitTmp = "";
double result = 0;
for (int j = 0; j < num; j++) {
String line = sc.nextLine();
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (Character.isDigit(c)) {
digitTmp += c;
} else {
if (digitTmp == "") {
continue;
}
double sumTmp = convert(digitTmp, c);
result += sumTmp;
digitTmp = "";
i += 2;
}
}
}
System.out.println((int)result);
}
private static double convert(String digitTmp, char c) {
double total = 0;
double amount = Integer.parseInt(digitTmp);
if (c == 'C') {
total += amount * 100;
} else if (c == 'J') {
total += amount * 10000 / 1825;
} else if (c == 'H') {
total += amount * 10000 / 123;
} else if (c == 'E') {
total += amount * 10000 / 14;
} else if (c == 'G') {
total += amount * 10000 / 12;
}else if (c == 'f') {
total += amount;
}else if (c == 'c') {
total += amount * 100 / 123;
}else if (c == 's') {
total += amount * 100 / 1825;
}else if (c == 'e') {
total += amount * 100 / 14;
}else if (c == 'p') {
total += amount * 100 / 12;
}
return total;
}
}
华为OD机试:10货币单位换算
最新推荐文章于 2024-09-13 16:27:15 发布
本文介绍了一个Java程序,用于处理输入的多国货币金额,如CNY、HKD、JPY等,并将其转换为人民币分,只保留整数部分。程序通过`Scanner`读取用户输入并调用`convert`方法进行换算。
摘要由CSDN通过智能技术生成