题解 | #人民币转换#

人民币转换

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

import java.util.*;
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String s = br.readLine();
            String[] split = s.split("\\.");
            Long num = Long.valueOf(split[0]);
            List<Long> list = new ArrayList();
            while (num != 0) {
                long yushu = num % 10000;
                list.add(yushu);
                num  = num / 10000;
            }
            List<String> subList = new ArrayList();
            subList.add("元");
            subList.add("拾");
            subList.add("佰");
            subList.add("仟");
            subList.add("万");
            subList.add("亿");
            subList.add("角");
            subList.add("分");
            subList.add("零");
            subList.add("整");
            Map<String, String> map = new HashMap();
            map.put("1", "壹");
            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("0", "零");
            StringBuffer stb = new StringBuffer();
            String temp = "";
            String cunfang = "";
            for (int i = 0; i < list.size(); i++) {
                int f = -1;
                int count = 0;
                long subNum = list.get(i);
                long beifen = list.get(i);
                if (subNum == 0) {
                    if (i == 0) {
                        temp = subList.get(0);
                    } else if (i == 1) {
                        temp = map.get("8") + temp;
                    }
                    continue;
                }
                if (i == 1 && count == 0) {
                    temp = subList.get(4) + temp;
                } else if (i == 2 && count == 0) {
                    temp = subList.get(5) + temp;
                }
                long a = 0;
                long b = 0;
                while (subNum != 0 || list.size() > i + 1 && count <= 3) {
                    count++;
                    a = subNum % 10;
                    f++;
                    if (a >= 1) {
                        if (a == 1 && count == 2) {
                            temp = subList.get(f) + temp;
                        } else {
                            if (i > 0 && count == 1) {
                                temp = map.get(String.valueOf(a)) + temp;
                            } else {
                                temp = map.get(String.valueOf(a)) + subList.get(f) + temp;
                            }
                        }
                    } else if (a == 0) {
                        if (count == 1) {
                            temp = subList.get(0);
                        } else if (a != b) {
                            temp = map.get(String.valueOf(a)) + temp;
                        }
                    }
                    b = subNum % 10;
                    subNum /= 10;
                }
            }
            if ("00".equals(split[1])) {
                temp = temp + subList.get(9);
            } else if (split[1].charAt(0) != '0' && split[1].charAt(1) != '0') {
                temp = temp + map.get(String.valueOf(split[1].charAt(0))) + subList.get(
                           6) + map.get(String.valueOf(split[1].charAt(1))) + subList.get(7);
            } else if (split[1].charAt(0) != '0') {
                temp = temp + map.get(String.valueOf(split[1].charAt(0))) + subList.get(6);
            } else {
                temp = temp + map.get(String.valueOf(split[1].charAt(1))) + subList.get(7);
            }
            System.out.println("人民币"+temp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

全部评论

相关推荐

08-16 10:51
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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