全网最全面的华为OD机试真题汇总,100%原题题库,不需要开会员即可查看全部内容,更多考题请查看真题库。
真题库:https://www.yuque.com/codernav.com/od
题目:货币单位换算
时间限制:1s 空间限制:256MB 限定语言:不限
题目描述:
记账本上记录了若干条多国货币金额,需要转换成人民币分(fen),汇总后输出。
每行记录一条金额,金额带有货币单位,格式为数字+单位,可能是单独元,或者单独分,或者元与分的组合。
要求将这些货币全部换算成人民币分(fen)后进行汇总,汇总结果仅保留整数,小数部分舍弃。
元和分的换算关系都是1:100,如下:
1CNY=100fen(1元=100分)
1HKD=100cents(1港元=100港分)
1JPY=100sen(1日元=100仙)
1EUR=100eurocents(1欧元=100欧分)
1GBP=100pence(1英镑=100便士)
汇率如下表
CNY
JPY
HKD
EUR
GBP
100
1825
123
14
12
即100CNY=1825JPY=123HKD=14EUR=12GBP
输入描述:
第一行输入为N,N表示记录数。0<N<100
之后N行,每行表示一条货币记录,且该行只会是一种货币。
输出描述:
将每行货币转换成人民币分(fen)后汇总求和,只保留整数部分。
输出格式只有整数数字,不带小数,不带单位。
补充说明:
输出格式只有数字,不带单位
示例1
输入:
1
100CNY
输出:
10000
说明:
100CNY转换后是10000fen,所以输出结果为10000
示例2
输入:
1
3000fen
输出:
3000
说明:
3000fen,结果就是3000
示例3
输入:
1
123HKD
输出:
10000
说明:
HKD与CNY的汇率关系是123:100,所以换算后,输出结果为10000
示例4
输入:
2
20CNY53fen
53HKD87cents
输出:
6432
说明:
20元53分+53港元87港分,换算成人民币分后汇总,为6432
解题思路:
因为5种货币的首字母不相同,所以可以将其作为货币的标识来进行换算。
计算的时候需要使用double类型,否则会出问题。
代码实现一:
package com.codernav.demo.hwod.exam;
import java.util.Scanner;
/**
* @title 货币单位换算
* @Description 记账本上记录了若干条多国货币金额,需要转换成人民币分(fen),汇总后输出。
* 每行记录一条金额,金额带有货币单位,格式为数字+单位,可能是单独元,或者单独分,或者元与分的组合。
* 要求将这些货币全部换算成人民币分(fen)后进行汇总,汇总结果仅保留整数,小数部分舍弃。
* @Author 开发者导航
* @website https://codernav.com
* @date 2023/5/13
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int res = 0;
sc.nextLine();
for (int i = 0; i < N; i++) {
res += getRes(sc.nextLine());
}
System.out.println(res);
}
public static double getRes(String str) {
StringBuilder temp = new StringBuilder();
double count = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c)) {
temp.append(c);
} else {
if (temp.toString() == "") { //temp为空,说明已经计算过了,直接跳过。用来避免四位及以上字符的简写货币(cents)
continue;
}
count += huansuan(temp.toString(), c);
i += 2; //因为货币的简写至少为3位,所以可以跳两位,缩减时间
temp = new StringBuilder();
}
}
return count;
}
public static double huansuan(String numStr, char str) {
double count = 0;
int num = Integer.parseInt(numStr);
if (str == 'C') {
count = num * 100;
} else if (str == 'J') {
count = (double) num * 10000 / 1825;
} else if (str == 'H') {
count = (double) num * 10000 / 123;
} else if (str == 'E') {
count = (double) num * 10000 / 14;
} else if (str == 'G') {
count = (double) num * 10000 / 12;
} else if (str == 'f') {
count = num;
} else if (str == 's') {
count = (double) num * 100 / 1825;
} else if (str == 'c') {
count = (double) num * 100 / 123;
} else if (str == 'e') {
count = (double) num * 100 / 14;
} else if (str == 'p') {
count = (double) num * 100 / 12;
}
return count;
}
}
代码实现二:下面是满分实现代码
package com.codernav.demo.hwod.exam;
import java.util.Scanner;
/**
* @title 货币单位换算
* @Description 记账本上记录了若干条多国货币金额,需要转换成人民币分(fen),汇总后输出。
* 每行记录一条金额,金额带有货币单位,格式为数字+单位,可能是单独元,或者单独分,或者元与分的组合。
* 要求将这些货币全部换算成人民币分(fen)后进行汇总,汇总结果仅保留整数,小数部分舍弃。
* @Author 开发者导航
* @website https://codernav.com
* @date 2023/5/13
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double money = 0.0;
int count = Integer.parseInt(sc.nextLine());
for (int i = 0; i < count; i++) {
String line = sc.nextLine();
String[] split = line.split("\\d");
for (String type : split) {
if (!type.isEmpty()) {
String num = line.substring(0, line.indexOf(type));
money += getCny(type, Double.parseDouble(num));
line = line.substring(line.indexOf(type) + type.length());
}
}
}
System.out.println(Math.round(Math.floor(money)));
}
private static double getCny(String type, double num) {
switch (type) {
case "CNY":
return num * 100;
case "fen":
return num;
case "JPY":
return num / 1825 * 10000;
case "sen":
return num / 1825 * 100;
case "HKD":
return num / 123 * 10000;
case "cents":
return num / 123 * 100;
case "EUR":
return num / 14 * 10000;
case "eurocents":
return num / 14 * 100;
case "GBP":
return num / 12 * 10000;
case "pence":
return num / 12 * 100;
}
return 0;
}
}
