PAT (Advanced Level) Practice 1005 Spell It Right (20 分) – Java 实现
Given a non-negative integer NNN, your task is to compute the sum of all the digits of NNN, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an NNN (≤10100\le 10^{100}≤10100).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
12345
Sample Output:
one five
解:
import java.util.Scanner;
public class Main {
public static final String[] DIGIT_ENG_ARY = new String[]{"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String digits = in.nextLine();
Integer num = 0;
for (Character digit : digits.toCharArray()) {
num += Integer.valueOf(digit.toString());
}
StringBuffer sb = new StringBuffer();
for (Character digit : num.toString().toCharArray()) {
String eng = DIGIT_ENG_ARY[Integer.valueOf(digit.toString())].toLowerCase();
sb.append("".equals(sb.toString()) ? eng : " " + eng);
}
System.out.println(sb.toString());
}
}
}
参考: