1005 Spell It Right (20point(s))
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各位的数字之和,然后英文输出结果的每一个数字。
解题思路:水题。
Java题解:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] arr = {"zero","one","two","three","four","five","six","seven","eight","nine"};
String s = sc.next();
int sum = 0;
for(int i=0; i<s.length(); i++)
{
sum += s.charAt(i)-'0';
}
String s2 = sum+"";
System.out.print(arr[s2.charAt(0)-'0']);
for(int i=1; i<s2.length(); i++)
{
System.out.print(" "+arr[s2.charAt(i)-'0']);
}
}
}
C++题解:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a;
cin >> a;
int sum = 0;
for(int i=0; i<a.length(); i++)
{
sum+=(a[i]-'0');
}
string s;
stringstream ss;
ss << sum;
ss >> s;
string arr[10] = {
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
};
cout << arr[s[0]-'0'];
for(int i=1; i<s.length(); i++)
cout << " " << arr[s[i]-'0'];
return 0;
}