1. 程式人生 > 其它 >1005 Spell It Right(20分)

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 }