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 (≤).

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

思路:用字符串存储数据(数据过大),对每一位求和并存入数组,倒序输出即可

#include<bits/stdc++.h>
using namespace std;
const int maxn=10010;
int nums[maxn];
void trance(int m){
    if(m==0){
        printf("zero");
    }
    else if(m==1){
        printf("one");
    }
    else if(m==2){
        printf("two");
    }
    else if(m==3){
        printf("three");
    }
    else if(m==4){
        printf("four");
    }
    else if(m==5){
        printf("five");
    }
    else if(m==6){
        printf("six");
    }
    else if(m==7){
        printf("seven");
    }
    else if(m==8){
        printf("eight");
    }
    else if(m==9){
        printf("nine");
    }
}
int main(){
    string n;
    cin>>n;
    if(n=="0"){
        printf("zero\n");
        return 0;
    }
    long long sum=0;
    for(int i=0;i<n.size();i++){
        sum+=n[i]-'0';
    }
    int i=0;
    while(sum>0){
        nums[i]=sum%10;
        sum/=10;
        i++;
    }
    for(int j=i-1;j>=0;j--){
        trance(nums[j]);
        if(j>0){
            printf(" ");
        }
        else{
            printf("\n");
        }
    }
    return 0;
}

 

posted @ 2021-06-18 22:35  XA科研  阅读(24)  评论(0编辑  收藏  举报