题解 | #人民币转换#

人民币转换

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;
    }
}
全部评论

相关推荐

那一天的Java_J...:他本来公司就是做这个的,不就是正常的游戏客户端和服务器开发,软硬件联动,有啥恶心不恶心的,提前告诉你就是怕你接受不了,接受不了就没必要再往后走流程浪费时间,虽然这公司是一坨。
点赞 评论 收藏
分享
05-22 09:23
门头沟学院 Java
点赞 评论 收藏
分享
字节一直是我的白月光,考虑到转正还是拒了日常实习。
从明天开始狠狠卷JV...:为什么你释放的offer没流到我头上
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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