|
发表于 2020-5-9 09:15:01
|
显示全部楼层
自认为无懈可击,愿高人测试。
受到nanase的启发,谨表示谢意,愿与大家共享思路。
public class RMBConvert {
private static String[] faceVal = new String[] { "零", "壹", "贰", "叁", "肆",
"伍", "陆", "柒", "捌", "玖" };
private static String[] level = new String[] { "圆", "拾", "佰", "仟", "萬", "亿" };
public static void main(String[] arg) {
StringBuffer sb = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
String s1 = in.readLine();
for (int i = 0; i < s1.length(); i++) {
int digit = Integer.valueOf(s1.substring(i, i + 1)).intValue();
sb.append(faceVal[digit]);
}
String face = sb.reverse().toString();
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < face.length(); i++) {
//加权
if (i == 0) {
resultSb.append(level[0]);
}
else{
//万位处理
if ((i + 4) % 8 == 0) {
resultSb.append(level[4]);
}
//亿位处理
else if (i % 8 == 0) {
resultSb.append(level[5]);
}
else {
resultSb.append(level[i % 4]);
}
}
//加面值
String temp = face.substring(i, i + 1);
resultSb.append(temp);
}
String result = resultSb.reverse().toString();
result = result.replaceAll("零拾", "零");
result = result.replaceAll("零佰", "零");
result = result.replaceAll("零仟", "零");
result = result.replaceAll("[零]+", "零");
result = result.replaceAll("零圆", "圆");
result = result.replaceAll("零萬", "萬");
result = result.replaceAll("零亿", "亿");
System.out.println(result + "整");
} catch (Exception e) {
e.printStackTrace();
}
}
} |
|