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 (<= 10^100^).
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
题目大意:
给定一个字符串,求字符串中的数字相加的和对应的英文字符。
分析:
获取字符串,将字符串中的每一位相加的值再转换成英文字符,输出
Java实现
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String digits[] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
String input = sc.next();
sc.close();
int sum = 0;
for(int i=0;i<input.length();i++){
sum += Integer.parseInt(String.valueOf(input.charAt(i)));
}
String temp = String.valueOf(sum);
String output ="";
for(int i=0;i<temp.length();i++){
if(i==temp.length()-1){
output += digits[Integer.parseInt(String.valueOf(temp.charAt(i)))];
}else {
output += digits[Integer.parseInt(String.valueOf(temp.charAt(i)))];
output += " ";
}
}
System.out.print(output);
}
}
Python实现
if __name__ == "__main__":
line = input()
num = sum([int(x) for x in line])
sums = str(num)
digits = ["zero","one","two","three","four","five",
"six","seven","eight","nine"]
result = ''
for x in sums:
result = result + digits[int(x)] + ' '
print(result[:-1])