首页 > 代码库 > PAT1002

PAT1002

读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100

输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。

输入样例:
1234567890987654321123456789
输出样例:
yi san wu
package com.lwh.agrithmatic.paractice;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
public class Practise2 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        BigInteger n=scanner.nextBigInteger();
        String str=n.toString();
        ArrayList<Integer> arrayList=new ArrayList<Integer>();
        for(int i=0;i<str.length();i++){
            arrayList.add(Integer.parseInt(str.subSequence(i, i+1).toString()));
        }
        
        System.out.println(calResult(arrayList));
        
    }
    public static String calResult(ArrayList<Integer> arrayList){
        String  result="";
        int sum=0;
        for(int i=0;i<arrayList.size();i++){
            sum+=arrayList.get(i);
        }
        String[] strings={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
        String sum2=Integer.toString(sum);
        for(int i=0;i<sum2.length();i++){
        if(i==sum2.length()-1){
            result+=(strings[Integer.parseInt(sum2.subSequence(i, i+1).toString())]);
        }else{
            result+=(strings[Integer.parseInt(sum2.subSequence(i, i+1).toString())]+" ");
        }
        }
        return result;
    }

}

 

PAT1002