题解 | #人民币转换#

人民币转换

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

import java.util.*;

public class Main{
    static String[] dx = {"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    static String[] dw = {"拾", "佰", "仟"};
    static String[] fs = {"角", "分"};
    public static void main(String[] args){

        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String str = in.next();
            String[] money = str.split("\\.");
            String res = "人民币";
            int len = money[0].length()/8;
            int index = money[0].length()%8;
            if(!money[0].equals("0")){
                for(int i=0; i<=len; i++){
                    if(i == 0){
                        res = res + yyx(money[0].substring(0, index));
                    } else {
                        res = res + yyx(money[0].substring(index+8*(i-1), index+8*i));
                    }
                    if(len>0&&i<len){
                        res = res + "亿";
                    } else {
                        res = res + "元";
                    }
                }
            }
            res = res + xsd(money[1]);
            System.out.println(res);
        }
    }

    public static String qyx(String str){
        if(str.isEmpty()) return null;
        int len = str.length();
        String res = "";
        for(int i=0; i<len; i++){
            int tem = Integer.parseInt(str.substring(i, i+1));
            if(tem == 0){
                if(i == 0 || (str.charAt(i-1)!='0' && i!=len-1)){
                    res = res + "零";
                }
            }else{
                if(tem == 1 && ((len > 2 && i==len - 2 && str.charAt(i-1)=='0') || (len == 2&&i==0))){
                    res = res + dw[len - i - 2];
                } else if(i == len -1){
                    res = res + dx[tem -1];
                } else {
                    res = res + dx[tem -1] + dw[len - i - 2];
                }

            }
        }
        return res;
    }

    public static String yyx(String str){
        String res = "";
        if(str.length() > 4){
            String wys = str.substring(0, str.length() - 4);
            String wyx = str.substring(str.length() - 4);
            res = res + qyx(wys) + "万" + qyx(wyx);
        } else {
            res = qyx(str);
        }
        return res;
    }

    public static String xsd(String str){
        int len = str.length();
        String res ="";
        for(int i=0; i<len; i++){
            int tem = Integer.parseInt(str.substring(i, i+1));
            if(tem != 0){
                res = res + dx[tem-1] + fs[i];
            }
        }
        if(res.isEmpty()) res = res + "整";
        return res;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 17:28
25届每天都在焦虑找工作的事情0offer情绪一直很低落硬撑着面了一个岗位岗位有应酬的成分面试的时候hr给我出各种场景题问的问题比较犀利&nbsp;有点压力面的感觉感觉有点回答不上来本来就压抑的情绪瞬间爆发了呢一瞬间特别想哭觉得自己特别没用没绷住掉眼泪了事后想想觉得自己挺有病的&nbsp;真的破大防了
喜欢唱跳rap小刺猬...:我觉得没关系吧,之前有一次面试leader给我压力面,我顶住了压力,结果入职的时候发现组里氛围很差,果断跑路。其实从面试就能大概看出组的情况,面试体验好的组倒是不一定好,但是面试体验不好的组。。。就很难说
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 12:22
点赞 评论 收藏
分享
废物一个0offer:认真的吗二本本科找人工智能岗位
点赞 评论 收藏
分享
程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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