题解 | #人民币转换#

人民币转换

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    private static Map<Integer,String> map;
    private static Map<Integer,String> map1;
    public static void main(String[] args) throws IOException {
        map=new HashMap<>();
        map1=new HashMap<>();
        map.put(2,"拾");
        map.put(3,"佰");
        map.put(4,"仟");
        map.put(5,"万");
        map.put(6,"拾万");
        map.put(7,"佰万");
        map.put(8,"仟万");
        map.put(9,"亿");
        map.put(10,"拾亿");
        map.put(11,"佰亿");
        map.put(12,"仟亿");
        map1.put(1,"壹");
        map1.put(2,"贰");
        map1.put(3,"叁");
        map1.put(4,"肆");
        map1.put(5,"伍");
        map1.put(6,"陆");
        map1.put(7,"柒");
        map1.put(8,"捌");
        map1.put(9,"玖");
        
        
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String res="人民币";
            String money=sc.nextLine();
            String[] arr=money.split("\\.");
            String pre=read(arr[0])+"元";
            if(pre.startsWith("壹")&&pre.length()>1&&pre.charAt(1)=='拾'){
                pre=pre.substring(1);
            }
            if(pre.length()==1){
                pre="";
            }
            if(arr.length==1){
                System.out.println(res+pre+"整");
            }
            else{
                String s=arr[1];
                String next="";
                if(map1.containsKey(Integer.valueOf(s.substring(0,1)))){
                    next+=map1.get(Integer.valueOf(s.substring(0,1)))+"角";
                }
                if(map1.containsKey(Integer.valueOf(s.substring(1,2)))){
                    next+=map1.get(Integer.valueOf(s.substring(1,2)))+"分";
                }

                System.out.println(res+pre+next);
            }
        }
    }
    private static String read(String money){
        if(!map.containsKey(money.length())){
            if(money.equals("0")){
                return "";
            }
            else{
                return map1.get(Integer.valueOf(money));
            }
        }
        else if(money.charAt(0)=='0'){
            int index=0;
            for(int i=1;i<=money.length()-1;i++){
                if(money.charAt(i)!='0'){
                    index=i;
                    break;
                }
            }
            return "零"+read(money.substring(index));
        }
        String danwei=map.get(money.length());
        int count=Integer.valueOf(money.substring(0,1));

        int len=1;
        if(danwei.length()>1){
            if(danwei.startsWith("仟")){
                len=4;
            }
            else if(danwei.startsWith("佰")){
                len=3;
            }
            else if(danwei.startsWith("拾")){
                len=2;
            }
        }
        String diff=money.substring(len);
        boolean b=true;
        for(int i=0;i<=diff.length()-1;i++){
            if(diff.charAt(i)!='0'){
                b=false;
            }
        }

        if(b){
            return read(money.substring(0,len))+danwei.substring(danwei.length()-1,danwei.length());
        }
        String pre=read(money.substring(0,len))+danwei.substring(danwei.length()-1,danwei.length());
        String next=read(diff);
        return pre+next;
    }
}
全部评论

相关推荐

04-08 23:14
已编辑
南阳理工学院 算法工程师
本人情况:26届双非本科,两段实习经历,目前拿到的都是实习的offer,一个校招的都没有,他们都说先实习,然后等拿到毕业证了直接转正,我又害怕干三个月给我叉出去面试题也发一下吧###&nbsp;杭州问尔信息技术后端登录你是怎么做的?JWT令牌你了解过吗?他虽然是一段字符串,他表达了什么东西?怎么解析出来信息和过期时间?JWT令牌怎么续期?如果我拉黑一个账号,要怎么做?两种方案(有无redis)mongodb和mysql的区别?mongodb和mysql分别实用于什么项目?选型你会怎么选?数据库的事务,那些地方需要使用,那些地方不需要使用?他会影响什么性能?mysql和pgsql有什么差异你知道吗?消息队列&nbsp;redis也有,为什么要用mq?前后端会部署吗?docker会用吗?内部通信前端&nbsp;async和&nbsp;await你知道吗?异步编程的原理是什么?vue3&nbsp;为什么你改变一个字符串&nbsp;前端会跟着改动AI工具会用什么?你会怎么用?###&nbsp;仲财通常用的锁有哪些synchronize和ReentrantLock的区别分布式锁了解吗?分布式事务mysql表字段sql优化什么时候用索引索引什么时候会失效mysql事务ioc一些项目应用问题###&nbsp;观妙科技项目问题...zset的架构是什么样子的线程池突然队列被打满了怎么办?如果上游和下游都无法控制,该怎么维护select&nbsp;*&nbsp;from&nbsp;user&nbsp;where&nbsp;age&gt;20&nbsp;order&nbsp;by&nbsp;update_time&nbsp;索引设计检索过程是什么样的冒泡排序和快排,有什么区别怎么判断链表有没有环###&nbsp;观妙科技-二面项目部分...线程池的核心参数有哪些你是怎么用线程池的JMMG1模型跳表介绍一下平衡二叉树TCP为什么要三次握手?说一下hashmap红黑树的特征你有什么学习的方法
牛马人的牛马人生:对学院本而言很强了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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