1005 Spell It Right(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

 

和1001一个类型的题,主要考察的就是字符串与数字类型间的转换,本题在两次类型转换后可以快速ac

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char digit[11][11]={"zero","one","two","three","four","five","six","seven","eight","nine"};
 4 int main(){
 5     char a[105];
 6     char ssum[105];
 7     cin>>a;
 8     int i=0;
 9     int sum=0;
10     while(a[i]){
11         sum+=a[i]-'0';
12         ++i;
13     }
14     sprintf(ssum,"%d",sum);
15     for(i=0;i<strlen(ssum);++i){
16         cout<<digit[ssum[i]-'0'];
17         if (i!=strlen(ssum)-1){
18             cout<<" ";
19         }
20     }
21 
22 }

 

posted on 2022-05-18 10:59  Coder何  阅读(18)  评论(0)    收藏  举报