小团数字转换问题
题目描述
就是将阿拉伯数字转换为中文大写数字
输入
一维字符串数组,每项为阿拉伯数字,数字保留 2 位且最大值不超过一万亿,
输出
转换后的数组
样例输入
["200.00","201.15","1015","200001010200"]
样例输出
["贰佰元整","贰佰零壹元壹角伍分","壹仟零壹拾伍元整","贰仟亿零壹佰零壹万零贰佰元整"]
思路
模拟!另外题目我看傻了!!!
编码
import java.util.*;
public class Main {
private static final String[] str = {"元", "拾", "佰", "仟", "万", "亿"};
private static final String[] num = {"零", "壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private static Deque<String> stack = new LinkedList<>();
private static boolean getPoint(String s) {
if(s.charAt(s.length()-3) == '.' && s.charAt(s.length()-2) != '0' && s.charAt(s.length()-1) != '0')
return true;
return false;
}
private static String getStr(int count) {
if(count < 5)
return str[count];
if(count < 8) {
return str[count-4];
}
if(count == 8) return str[5];
return str[count-8];
}
private static String getNum(int count) {
return num[count];
}
private static String change(String s) {
boolean point = getPoint(s);
int p = s.charAt(s.length()-3) == '.' ? s.length()-3 : s.length();
if(point) {
int fen = s.charAt(s.length()-1)-'0', jiao = s.charAt(s.length()-2)-'0';
if(fen > 0) {
stack.push(getNum(fen) + "分");
}
if(jiao > 0) {
stack.push(getNum(jiao) + "角");
}
} else {
stack.push("整");
}
boolean flag = false;
for(int j = 0, i = p-1; i >= 0; --i, ++j) {
int x = s.charAt(i)-'0';
if(x != 0) {
stack.push(getStr(j));
stack.push(getNum(x));
flag = true;
} else if(j == 0 || j == 4 || j == 8) {
stack.push(getStr(j));
}else if(flag) {
stack.push(getNum(x));
flag = false;
}
}
StringBuilder res = new StringBuilder();
while(!stack.isEmpty()) {
res.append(stack.pop());
}
return res.toString();
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String input = scn.next(), head = "["", splitStr = "","", tail = ""]";
input = input.substring(2, input.length()-2);
String[] list = input.split(splitStr);
for(int i = 0; i < list.length; ++i)
list[i] = change(list[i]);
System.out.print(head);
System.out.print(list[0]);
for(int i = 1; i < list.length; ++i) {
System.out.print(splitStr);
System.out.print(list[i]);
}
System.out.println(tail);
}
}