PAT甲级1005

1005. Spell It Right (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.


Sample Input:
12345
Sample Output:

one five

以上是题目

以下是代码

#include
#include
#include
#include

using namespace std;

int main(){
    string number;
    vector eng;
     int sum;
    cin >> number;
    string tag[ 10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
     for(string::iterator i = number. begin();i!=number. end();i++){
sum += *i- '0';
    }
     if(sum == 0){
        cout << "zero"<< endl;
return 0;
    }
     while(sum != 0){
        eng. push_back(tag[sum% 10]);
        sum /= 10;
    }
     reverse(eng. begin(),eng. end());
    cout << eng[ 0] ;
     for(vector::iterator i = eng. begin()+ 1;i!= eng. end();i++){
        cout << " "<< *i;
    }
    cout << endl;
     return 0;
}
小结:没有什么算法,简单的逻辑题,需要注意边界值N=0的情况需要分开讨论,此外N的值范围太大,longlong都表示不了,需要用字符串表示,其他应该就没什么坑了(很不幸,我都踩了。。。。)。

你可能感兴趣的:(PAT)