您的位置:首页 > 其它

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

2017-03-08 17:33 330 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: