Given a non-negative integer N, your task is to compute the sum of all the digits of N, 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 N (≤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
解题思路:
题目大意为给个大整数,求每位数字之和并用英文输出。
可以将整数转为字符串,将和也转为字符串,遍历字符串时可以用switch转化,也可以节省篇幅,用字符串数组转换。
java代码:
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
StringBuilder builder = new StringBuilder();
int sum = 0;
for(int i = 0; i < str.length();i++) {
sum += Integer.parseInt(str.charAt(i) + "");
}
String s = sum + "";
for(int i = 0; i < s.length();i++) {
builder.append(convertToString(s.charAt(i)));
}
System.out.print(builder.toString().trim());
}
public static String convertToString(char c) {
switch (c) {
case '0':return "zero ";
case '1':return "one ";
case '2':return "two ";
case '3':return "three ";
case '4':return "four ";
case '5':return "five ";
case '6':return "six ";
case '7':return "seven ";
case '8':return "eight ";
case '9':return "nine ";
default:
return "";
}
}
}
PAT提交截图: