题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
String[] strs = s.split("\\."); String a = strs[0]; Map<Character, String> map = new HashMap<>(); Map<Integer, String> map1 = new HashMap<>(); map.put('0', "零"); 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', "玖"); map1.put(2, "拾"); map1.put(3, "佰"); map1.put(4, "仟"); map1.put(5, "万"); map1.put(6, "拾"); map1.put(7, "佰"); map1.put(8, "仟"); map1.put(9, "亿"); //map1.put(6, "拾万"); int index = 0; StringBuilder sb = new StringBuilder(); if (!a.equals("0")) { for (int i = a.length() - 1; i >= 0; i--) { index++; String s1 = map.get(s.charAt(i)); if (i != a.length() - 1 && sb.length() > 1 && sb.charAt(0) == '零' && s1.equals("零")) { continue; } if (s1.equals("零")) { sb.insert(0, s1); continue; } if (map1.get(index) != null) { if (s1.equals("壹") && map1.get(index).equals("拾")) { s1 = ""; } s1 += (map1.get(index)); } sb.insert(0, s1); } sb.append("元"); } String b = strs[1]; if (!b.equals("00")) { if (b.charAt(0) != '0') { sb.append(map.get(b.charAt(0))); sb.append("角"); } if (b.charAt(1) != '0') { sb.append(map.get(b.charAt(1))); sb.append("分"); } } else { sb.append("整"); } sb.insert(0, "人民币"); System.out.println(sb.toString()); }
}