美文网首页
1005 Spell It Right

1005 Spell It Right

作者: GloryXie | 来源:发表于2019-06-09 22:14 被阅读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 (≤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

思路

本身题目难度较低,题意是给予一个大小在10100之内的自然数N,将其各位相加并用英文输出。由于10100的大小已经超出int型所能存储的范围,因此采用string进行存储,并将各位相加,然后再存储到一个数中,并对这个数进行各位分解然后逆序输出即可。

代码

//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 (≤10
//                                                                                         ​100
//                                                                                         ​​ ).
//
//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.
#include <iostream>
using namespace std;
string toEnglish(int N){
    if(N==0)return "zero";
    else if (N==1)return "one";
    else if (N==2)return "two";
    else if (N==3)return "three";
    else if (N==4)return "four";
    else if (N==5)return "five";
    else if (N==6)return "six";
    else if (N==7)return "seven";
    else if (N==8)return "eight";
    else if (N==9)return "nine";
    else return "";
}
int main(void) {
    int s=0,sum[905],i=905;
    string N;
    cin>>N;
    
    if(N =="0")cout<<"zero";
    for (int i=0; i<N.length(); i++) {
        s+=N[i]-'0';
    }
    while (s>0) {
        sum[i]=s%10;
        s/=10;
        i--;
    }
    cout<<toEnglish(sum[i+1]);
    i++;
    
    while (i<905) {
        cout<<' '<<toEnglish(sum[i+1]);
        i++;
    }
}

相关文章

网友评论

      本文标题:1005 Spell It Right

      本文链接:https://www.haomeiwen.com/subject/emroxctx.html