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 (<= 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:12345Sample Output:
one five
解题报告:简单的字符串操作,题目意思是给一个数字字符串(长度最长100位),计算各位之和sum,并将sum各位从高位到低位每位用英文表示。例如:12345 各位相加等于15,15的每一位用英文表示,得到one five
代码如下:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
string s;
int sum = 0;
cin>>s;
string str[10] =
{
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
};
// 计算数字字符串每位之和sum
for(int i = 0; i < s.length(); i ++)
sum = sum + s[i] - '0';
if(sum == 0)
cout<<"zero";
vector<string> vs;
// 将sum的每一位英文保存到vector中
while(sum)
{
vs.push_back(str[sum%10]);
sum /= 10;
}
reverse(vs.begin(), vs.end());
vector<string>::iterator it;
for(it = vs.begin(); it != vs.end(); it ++)
{
if(it == vs.begin())
cout<<*it;
else
cout<<" "<<*it;
}
cout<<endl;
return 0;
}