1005 Spell It Right (20)(20 分)
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
简析:
题干很简单,输入一串数字(没有空格),用英文输出它们的和的每一位。
方法是简单的hash思想,问题在于怎样单个处理输入的字符串,用下面的办法行不通。
while(c=cin.get()!=EOF){}//c++ 11没有gets()了
从stringstream的用法这里学到了stringstream用来转换字符串和数字的技巧。
不得不说这样大大简化了字符串处理的难度,以后将总结字符串处理的一些内容。
code:
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
string code[10] = {"zero", "one", "two", "three", "four","five", "six", "seven", "eight", "nine"};
int main(){
int n=0;
string s, str;
cin >> s;
for(unsigned int i=0;i<s.size();i++)
{
n += (s[i] - '0');
}
stringstream ss;
ss << n;
ss >> str;
cout << code[str[0]-'0'];
for(unsigned int i=1;i<str.size();i++)
{
cout << " " << code[str[i]-'0'];
}
cout << endl;
system("pause");
return 0;
}