题解 | #人民币转换#

人民币转换

https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b

import java.util.*;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static Map<Integer, String> map = new HashMap<Integer, String>() {
        {
            put(0, "零");
            put(1, "壹");
            put(2, "贰");
            put(3, "叁");
            put(4, "肆");
            put(5, "伍");
            put(6, "陆");
            put(7, "柒");
            put(8, "捌");
            put(9, "玖");
            put(10, "拾");
            put(100, "佰");
            put(1000, "仟");
        }
    };

    private static Map<Integer, String> map1 = new HashMap<Integer, String>() {
        {
            put(1, "元");
            put(10000, "万");
            put(100000000, "亿");
        }
    };

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double money = in.nextDouble();
        System.out.println(transfer(money));
    }

    public static String transfer(double money) {
        String result = "";
        double n = 1;
        money = Math.round(money *
                           100);// 防止精度丢失 todo 坑:0.29-》0.289999999999,double 类型浮点数精度丢失
        while (money > 0) {
            int c = (int) (money % 10);
            if (n < 100) {//处理 角 分
                if (c > 0) result = appendBefore(result, map(c), n == 1 ? "分" : "角");
            } else {
                n /= 100;
                result = appendBefore(result, c > 0 ? map(c) : "", map1(n));// 元 万 亿
                int n1 = 10;
                for (int i = 0; i < 3 && money > 0; ++i) {
                    n *= 10;
                    money = Math.floor(money / 10);
                    if (money <= 0) {
                        break;
                    }
                    double c1 = money % 10;
                    if (c1 > 0) {
                        boolean b = c1 == 1 && n1 == 10;// 10->拾;100-> 一百
                        result = appendBefore(result, b ? "" : map(c1), map(n1));
                    } else {
                        char c2;
                        if ((c2 = result.charAt(0)) != '零' && !map1.containsValue(c2)) {
                            result = appendBefore(result, map(0));
                        }
                    }
                    n1 *= 10;
                }
                n *= 100;
            }
            n *= 10;
            money = Math.floor(money / 10);
        }
        if (result.endsWith("元")) result = appendBefore(result, "整");
        if (result.isEmpty()) result = appendBefore(result, map(0), "元");
        result = appendBefore(result, "人民币");
        return result;
    }

    private static String appendBefore(String origin, String... strs) {
        return String.join("", strs) + origin;
    }

    private static String map(double n) {
        return map.get((int) n);
    }

    private static String map1(double n) {
        return map1.get((int) n);
    }
}

说下思路:

在西方的数字体系中,K(koli), M(Million),G(Giga),T(Tera), 以 1000 为进制差。

而在我们的数字体系中,只有亿,以 10000 为进制差。

因此,我们可以 4 位一个循环,补位一个汉字:亿、万、元(刚好1后面什么都不用接)。每个循环内部逻辑通用。数字和汉字的转换直接用一个 map 封装,随用随取,堪称完美!

注意点:

1、映射数据结构为:map<Integer,String> ,多数时候我们用的是 double,直接用 double 是取不出来的,map(double n){ ...get((int)n)...}, 这种写法可以兼容 int 和 double.

2、double 小数存在失真问题(所有数字都以二进制存储,二进制无法精确表示所有小数),做一次转换更舒服:money=Math.round(money *100);

3、appendBefore(String origin, String... strs) ,转递可变参数有性能缺陷,对性能有要求的场景不介意这种写法。

全部评论

相关推荐

码农索隆:这种hr,建议全中国推广
点赞 评论 收藏
分享
程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
就前几天旅游的时候,打开抖音就经常刷到这类视频:以前是高学历学生、老师、主持人,现在做着团播、擦边主播的工作,以及那些经过精心包装的“职业转型”故事——从铺天盖地的VLOG到所谓的“04年夜场工作日记”,这些内容在初中升学、高考放榜等关键时间节点持续发酵。可以说非常直接且精准地在潜移默化地影响着心智尚未成熟的青少年,使其对特殊行业逐渐脱敏。那我就想问了:某些传播公司、平台运营者甚至某些夜场的老板,你们究竟在传递怎样的价值观?点开那些视频,评论区里也是呈现明显的两极分化:一种是​​经济下行论​​:“现在就业市场已经艰难到这种程度了吗?”​​一种是事实反驳派​​:这些创作者往往拥有名校背景,从事着...
牛客刘北:被环境教育的,为了能拿到足够的钱养活自己,不甘心也得甘心,现在的短视频传播的思想的确很扭曲,但是很明显,互联网玩上一年你就能全款提A6,但你全心全意不吃不喝工作一年未必能提A6,但是在高考中考出现这个的确很扭曲,在向大家传播“不上学,玩互联网也可以轻松年入百万”,不是人变了,是社会在变
预测一下26届秋招形势
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务