题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
import java.util.Scanner; public class Main { public static String[] ten = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; public static String[] power = {"万", "亿"}; public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); while (in.hasNext()) { String[] arr = in.nextLine().split("\\."); ; if (arr[1].equals("00")) { System.out.println("人民币" + solveBeforeDot(arr[0]) + "元整"); } else if (arr[0].equals("0")) { System.out.println("人民币" + solveAfterDot(arr[1])); } else { System.out.println("人民币" + solveBeforeDot(arr[0]) + "元" + solveAfterDot( arr[1])); } } } public static String solveBeforeDot(String str) { int intPart = Integer.parseInt(str); StringBuilder sb = new StringBuilder(); int pow = -1; while (intPart != 0) { if (pow != -1) { sb.append(power[pow]); } //sb.append(power[pow]); // less than 10000 int small = intPart % 10000; int gewei = small % 10; int shiwei = small / 10 % 10; int baiwei = small / 100 % 10; int qianwei = small / 1000 % 10; if (gewei != 0) { sb.append(ten[gewei]); } if (shiwei != 0) { sb.append("拾"); if (shiwei != 1) { sb.append(ten[shiwei]); } } else { if (gewei != 0 && (small > 99 || intPart > 10000)) { sb.append(ten[0]); } } if (baiwei != 0) { sb.append("佰"); sb.append(ten[baiwei]); } else { if (shiwei != 0 && (small > 999 || intPart > 10000)) { sb.append(ten[0]); } } if (qianwei != 0) { sb.append("仟"); sb.append(ten[qianwei]); } else { if (baiwei != 0 && (intPart > 10000)) { sb.append(ten[0]); } } intPart /= 10000; pow++; if (pow > 1) pow = 0; } return sb.reverse().toString(); } public static String solveAfterDot(String str) { StringBuilder sb = new StringBuilder(); int jiao = Integer.parseInt(str.substring(0, 1)); int fen = Integer.parseInt(str.substring(1)); ; if (jiao != 0) { sb.append(ten[jiao] + "角"); } if (fen != 0) { sb.append(ten[fen] + "分"); } return sb.toString(); } }