PAT-B-1002--写出这个数

Question

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

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

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

输入样例:

1
1234567890987654321123456789

输出样例:

1
yi san wu

Requirements

Time: 400ms

Space: 64MB

Length of Code: 16KB

Answer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.Scanner;
import java.util.Stack;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String str = in.nextLine();
in.close();
int res = 0;
for(int i=0; i<str.length(); i++)
res += str.charAt(i) - '0';
Stack<String> stack = new Stack<>();
do{
int temp = res%10;
switch(temp){
case(0):
stack.push("ling");
break;
case(1):
stack.push("yi");
break;
case(2):
stack.push("er");
break;
case(3):
stack.push("san");
break;
case(4):
stack.push("si");
break;
case(5):
stack.push("wu");
break;
case(6):
stack.push("liu");
break;
case(7):
stack.push("qi");
break;
case(8):
stack.push("ba");
break;
case(9):
stack.push("jiu");
break;
}
res /= 10;
}while(res!=0);
System.out.print(stack.pop());
while (!stack.isEmpty())
System.out.print(" " + stack.pop());
}
}

Running time: 114 ms

One thing worth mentioning: Using do{ }(while)