1002 写出这个数 (20 分)
读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
输入格式:
每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 10100
输出格式:
在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。
输入样例:
1234567890987654321123456789
输出样例:
yi san wu
思路
测试一个用例,范围为1~10100 然而int 范围为 -231~231-1
即-2147483648——2147483647 显然范围是不够的,决定采用bigInteger
代码如下
import java.math.BigInteger;
import java.util.Scanner;
public class p1002 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger a0 = new BigInteger("1");
BigInteger a = sc.nextBigInteger();
BigInteger a2 = new BigInteger("0");
BigInteger an = new BigInteger("10");
//求出数字各位和
while(a.compareTo(a0)!=(-1)){
BigInteger d = new BigInteger("10");
BigInteger m =a.mod(d);
a2=a2.add(m);
a=a.divide(d);
}
int dd = a2.intValue();
sys(dd);
int m = dd%10;
if(m==1){
System.out.print("yi");
}
if(m==2){
System.out.print("er");
}
if(m==3){
System.out.print("san");
}
if(m==4){
System.out.print("si");
}
if(m==5){
System.out.print("wu");
}
if(m==6){
System.out.print("liu");
}
if(m==7){
System.out.print("qi");
}
if(m==8){
System.out.print("ba");
}
if(m==9){
System.out.print("jiu");
}if(m==0){
System.out.print("ling");
}
}//输出
static void sys(int a){
int numberOfDigits = String.valueOf(a).length();
while(numberOfDigits>1){
double n = 0;
double c=0;
n=a/(Math.pow(10,numberOfDigits-1));
int m =(int)n;
c=a%(Math.pow(10,numberOfDigits-1));
a=(int)c;
numberOfDigits--;
if(m==1){
System.out.print("yi"+" ");
}
if(m==2){
System.out.print("er"+" ");
}
if(m==3){
System.out.print("san"+" ");
}
if(m==4){
System.out.print("si"+" ");
}
if(m==5){
System.out.print("wu"+" ");
}
if(m==6){
System.out.print("liu"+" ");
}
if(m==7){
System.out.print("qi"+" ");
}
if(m==8){
System.out.print("ba"+" ");
}
if(m==9){
System.out.print("jiu"+" ");
}if(m==0){
System.out.print("ling"+" ");
}
}
}
}