华为OD机试:10货币单位换算

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;
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值