PAT Class B Java Implementation _1002. Write this number _ with detailed problem-solving notes _02

1002. Write the number (20)

time limit
400 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CHEN, Yue

Read in a natural number n, calculate the sum of its digits, and write each digit of the sum in Chinese Pinyin.

Input format: Each test input contains 1 test case, that is, the value of the natural number n is given. Here n is guaranteed to be less than 10 100 .

Output format: output each digit of the sum of the digits of n in one line, there is a space between the pinyin numbers, but there is no space after the last pinyin number in a line.

Input sample:
1234567890987654321123456789
Sample output:
yi san wu


import java.util.Scanner;
public class PAT_B_1002//In the PAT submission, the class name needs to be changed to Main
{
	public static void main(String args[])
	{
		//Define a pinyin string array to store the pinyin corresponding to the number.
		String[] pinYin = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
		
		Scanner in = new Scanner(System.in);
		String numString = in.next();//Because the number is very large, reading it with an integer will overflow, so store the read number in the form of a string
		
		int sum = 0;//Calculate the sum of the digits of the input number
		for(int i = 0; i < numString.length(); i++)
		{
			sum = sum + (numString.charAt(i) - 48);//According to the ASCII code, converting characters to numbers requires -48
		}
		
		String sumString = sum + "";//Convert the sum of the digits into a string and use it as the index of the pinyin array
		for(int i = 0; i < sumString.length(); i++)
		{
			if(i != 0)
				System.out.print(" ");//Formatted output, no spaces before the first output
			System.out.print(pinYin[sumString.charAt(i) - 48]);//Output the result
		}
		
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325785166&siteId=291194637