PAT乙级:1002

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"+" ");
            }

        }


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wvdon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值