即使我检查了许多边境案例,我也没有得到我的代码出错的地方
import java.util.*;
class fun
{
String a[] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String b[] = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
String dis(int n)
{
String s ="";
if(n/100>0)
s+=a[n/100]+" Hundred ";
if(n%100/10==0)
s+=a[n%10];
else if(n%100/10==1)
s+=a[n%100];
else
s+=b[n%100/10]+" "+a[n%10];
return s;
}
}
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt(), a, b, c, d, e;
fun f = new fun();
while(t-->0)
{
String Str="";
long n = sc.nextLong();
a=(int)(n/Math.pow(10,12));
b=(int)(n/Math.pow(10,9)%1000);
c=(int)(n/Math.pow(10,6)%1000);
d=(int)(n/Math.pow(10,3)%1000);
e=(int)(n%1000);
if(a==1)
Str = "One Trillion";
else
{
if(b!=0)
Str+=f.dis(b)+" Billion ";
if(c!=0)
Str+=f.dis(c)+" Million ";
if(d!=0)
Str+=f.dis(d)+" Thousand ";
Str+=f.dis(e);
}
System.out.println(Str);
}
}
}
如果输入是:
1
104382426112
然后输出是:
一百四十亿三百八十二百万四十二万六千一百十二
答案 0 :(得分:2)
我能看到的唯一问题是这个代码不适用于大于Integer.MAX_VALUE的数字......这是2,147,483,647。那是20亿还是一点。
这种限制的原因是您使用int
进行整数运算。解决方案:改为使用long
。