循环-19. 币值转换(20)
时间:2014-06-20 12:47:46
收藏:0
阅读:149
输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式。如23108元,转换后变成“贰万叁仟壹百零捌”元。为了简化输出,用小写英文字母a-j顺序代表大写数字0-9,用S、B、Q、W、Y分别代表拾、百、仟、万、亿。于是23108元应被转换输出为“cWdQbBai”元。
输入格式:
输入在一行中给出一个不超过9位的非负整数。
输出格式:
在一行中输出转换后的结果。注意“零”的用法必须符合中文习惯。
输入样例1:813227345输出样例1:
iYbQdBcScWhQdBeSf输入样例2:
6900输出样例2:
gQjB
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
char[] a = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };
char[] b = { 'S', 'B', 'Q', 'W', 'Y' };
Scanner cin = new Scanner(System.in);
String str = cin.nextLine();
boolean flag = false;
StringBuffer sb = new StringBuffer();
for (int i = 0, j = str.length(); i < str.length(); i++, j--) {
if (str.charAt(i) == '0') { // 测试点4 101
flag = true;
}
if (str.charAt(i) != '0' || str.length() == 1) { // 测试点3 0
if (flag) {
if (str.length() > 5) { //测试点2 100001
sb.append(b[3]);
}
sb.append(a[0]);
flag = false;
}
if (str.charAt(i) != '0') { // 测试点4
sb.append(a[str.charAt(i) - '0']);
}
if (j == 9) {
sb.append(b[4]);
} else if (j == 8 || j == 4) {
sb.append(b[2]);
} else if (j == 7 || j == 3) {
sb.append(b[1]);
} else if (j == 6 || j == 2) {
sb.append(b[0]);
} else if (j == 5) {
sb.append(b[3]);
}
}
}
System.out.println(sb);
}
}
评论(0)