1005. Spell It Right (20) 数字之和转英文输出

来源:互联网 发布:认字软件 编辑:程序博客网 时间:2024/06/10 20:26

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

题目比较简单,唯一要注意的是对于sum==0这种情况的特殊处理,编程很多时候都有特殊情况,代码的完整性很重要,编程时除了要考虑基本功能,还应该注意边界处理,特殊情况,非法输入之类的情况。
另外,要多用vector,相比于数组它不需要初始化内存长度,很方便。

#include <iostream>#include <string>#include <vector>using namespace std;int main(void){    string English[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };    string input;    vector<int> sumNum;    cin >> input;    int sum = 0;    for (int i = 0; i < input.length(); i++)    {        sum += (int)(input[i] - '0');    }    int j = 0;    if (sum == 0)        cout << English[0];    while (sum)    {        sumNum.push_back(sum % 10);        ++j;        sum /= 10;    }    while (j)    {        if (j>1)            cout << English[sumNum[j - 1]] << " ";        else            cout << English[sumNum[j - 1]];        --j;    }    system("pause");    return 0;}
0 0