7-23 币值转换 (20 分)
输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式。如23108元,转换后变成“贰万叁仟壹百零捌”元。为了简化输出,用小写英文字母a-j顺序代表大写数字0-9,用S、B、Q、W、Y分别代表拾、百、仟、万、亿。于是23108元应被转换输出为“cWdQbBai”元。
输入格式:
输入在一行中给出一个不超过9位的非负整数。
输出格式:
在一行中输出转换后的结果。注意“零”的用法必须符合中文习惯。
输入样例1:
813227345
输出样例1:
iYbQdBcScWhQdBeSf
输入样例2:
6900
输出样例2:
gQjB
Java 代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static int[] split(int num) {
int[] array = new int[3];
for (int i = 0; i < array.length; i++) {
array[i] = num % 10000;
num /= 10000;
}
return array;
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(in.readLine());
StringBuilder sb = new StringBuilder();
int[] array = split(num);
String[] unit = { "", "S", "B", "Q" };
// 用于输出每千位中间0,并且只输出一次
boolean f = true;
for (int i = 0; i < 3; i++) {
int x = array[i];
int idx = 0;
// 万位单位和亿位单位
if (i == 1 && x > 0) {
sb.insert(0, "W");
} else if (i == 2 && x > 0) {
sb.insert(0, "Y");
}
int cnt = 0;
// 末尾多0的情况,只需要判断最后一位是不是0
int end = x % 10;
// 每千位小于10的数就不考虑0的输出
boolean isLess = x < 10;
while (cnt < 4) {
char c = (char) (x % 10 + 97);
if (f && c == 'a' && i < 2 && end != 0 && !isLess) {
sb.insert(0, 'a');
f = false;
}
sb.insert(0, c > 'a' ? c + unit[idx] : "");
x /= 10;
idx++;
cnt++;
}
}
System.out.println(sb.length() > 0 ? sb : 'a');
}
}