您的位置:首页 > 其它

1005. Spell It Right (20)

2017-02-01 21:40 399 查看
这个题简单,就直接贴在这吧,注意一点输入数据规模比较大,要用char来存储

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

// 1005. Spell It Right.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>

using namespace std;

void printSpelling(int number){
switch (number)
{
case 0:
cout << "zero";
break;
case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
case 3:
cout << "three";
break;
case 4:
cout << "four";
break;
case 5:
cout << "five";
break;
case 6:
cout << "six";
break;
case 7:
cout << "seven";
break;
case 8:
cout << "eight";
break;
case 9:
cout << "nine";
break;
default:
break;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<char> input;
char temInput;
//输入
while (true)
{
temInput = cin.get();
if (temInput == '\n')
break;
input.push_back(temInput);
}
//求和
int sum = 0;
for (int i = 0; i < input.size(); i++)
sum += input[i] - '0';

//输出
vector<int> output;
if (sum == 0)
output.push_back(0);
else
while (sum > 0)
{
output.push_back(sum % 10);
sum = sum / 10;
}
//打印
for (int i = 0; i < (int)output.size() - 1; i++){
printSpelling(output[output.size()-i-1]);
cout << " ";
}
//打印最后一个
if (output.size() > 0)
printSpelling(output[0]);
system("pause");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: