Spell It Right
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
题意:给出一个非负整数N,计算N的各个数位相加的和,输出和的每一位的英文读音~~
思路:将非负整数N用字符串形式输入,之后将每一个字符转化成数字相加,得到各个数位之和;然后将和转化成字符串形式,用英文输出!
c++代码
#include<iostream>
using namespace std;
int main(){
string s;
int n, flag = 0;
string sum[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
cin >> s;
for(int i = 0; i < s.length(); i++){
n += s[i]-'0';
}
string s1 = to_string(n);
for(int i = 0; i < s1.length(); i++){
if(flag){
cout << " ";
}
cout << sum[s1[i]-'0'];
flag = 1;
}
return 0;
}
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])
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);
}
}