首页 > 其他 > 详细

1005 Spell It Right (20 分)

时间:2021-06-18 23:50:10      阅读:25      评论:0      收藏:0      [点我收藏+]

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;
}

 

1005 Spell It Right (20 分)

原文:https://www.cnblogs.com/dreamzj/p/14901484.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!