小團數字轉換問題
題目描述
就是將阿拉伯數字轉換為中文大寫數字
輸入
一維字符串數組,每項為阿拉伯數字,數字保留 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);
}
}