小米OJ題目:原題點這裏
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
String str;
String[] num = {"零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖"};
while (scan.hasNextLine()) {
str = scan.nextLine().trim();
if (str.equals("0")) {
System.out.println("零元整");
continue;
}
int len = str.length();
String ans = "";
for (int i = 0; i < len; i++) {
int x = str.charAt(i) - '0';
if ((len - i) % 4 == 0) {
if (x != 0) ans += num[x] + "仟";
else if (str.charAt(i + 1) != '0') ans += "零";
}
if ((len - i) % 4 == 1) {
if (x != 0) {
if ((len - i) == 1) ans += num[x];
else if ((len - i) == 5) ans += num[x] + "萬";
else if ((len - i) == 9) ans += num[x] + "億";
} else {
if ((len - i) == 5) ans += "萬";
else if ((len - i) == 9) ans += "億";
}
}
if ((len - i) % 4 == 2) {
if (x != 0) ans += num[x] + "拾";
else if (str.charAt(i + 1) != '0') ans += "零";
}
if ((len - i) % 4 == 3) {
if (x != 0) ans += num[x] + "佰";
else if (str.charAt(i + 1) != '0') ans += "零";
}
}
ans = ans.replace("億萬","億");
ans+="元整";
System.out.println(ans);
}
}
}
藍橋杯基礎訓練題目:試題 基礎練習 數的讀法
這題目缺少一些比較特殊的測試數據,所以很敷衍都能AC了,如以下的代碼(如1000000就不行了)。暫時沒有找到好的辦法解決,由於yi(億)和yi(壹)同音,所以有點難搞,如果有大佬能解決的,跪求方法。
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;
public class Main {
static String [] num={"ling","yi","er","san","si","wu","liu","qi", "ba","jiu"};
static String unit[]={"","","shi","bai","qian","wan","shi","bai","qian","yi","shi"};
public static void main(String [] args){
Scanner sc =new Scanner(System.in);
String str = sc.next();
int n = str.length();
for(int i=0;i<n;i++){
int x = str.charAt(i)-'0';
if(x==0){
if((n-i)==5||(n-i)==9){
System.out.print("shi"+" ");
}
if((n-i)!=1&&str.charAt(i+1)!='0'){
System.out.print("ling"+" ");
}
}else if((n==2||n==6||n==10)&&x==1&&i==0){
System.out.print("shi"+" ");
}else{
System.out.print(num[x]+" "+unit[n-i]+" ");
}
}
}
}