1005 Spell It Right (20)(20 分)
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
分析:使用char或string类型存储数字,在转换一下求和就行
#include <bits/stdc++.h>
using namespace std;
int main()
{
string words[] = {"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"};
char str[10100];
cin >> str;
int len = strlen(str);
if(len == 1 && str[0] == '0')
cout << "zero" << endl;
int i, sum = 0, cnt = 0;
for(i = 0; i < len; i++)
sum = sum + int(str[i]) - 48;
int arr[10100];
while(sum != 0){
arr[cnt++] = sum%10;
sum /= 10;
}
for(i = cnt - 1; i >= 0; i--){
cout << words[arr[i]];
if(i != 0)
cout << " ";
}
return 0;
}
网友评论