1005. Spell It Right (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:12345Sample Output:
one five
/***************************************************88
@ AUTHOR: GAOMINQUAN
@ MAIL : [email protected]
@ DATA : 2014 - 2 - 22
**************************************************/
#include<iostream>
#include<vector>
#include<string>
using namespace std;
string words[11] = {"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
int digitSum(string num){
int sum = 0;
for(int stringIndex = 0; stringIndex <num.length(); stringIndex++){
sum += num[stringIndex] - '0';
}
return sum;
}
void split(int num){
vector<int> splitNum;
if( num == 0){
splitNum.push_back(0);
}else{
while(num>0){
int tail = num%10;
splitNum.push_back(tail);
num /= 10;
}
}
for(int splitIndex = splitNum.size() - 1; splitIndex > 0; splitIndex--){
cout<<words[splitNum[splitIndex]]<<" ";
}cout<<words[splitNum[0]];
}
int main(){
string num = "1";
cin>>num;
int sum = digitSum(num);
split(sum);
return 0;
}
NOTE:
D:\MSDev98\Bin\CODE\PAT_AL_1005.CPP(34) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable co
1. 在使用string的時候,要記得把string先導入才行;
2. 數字非常長的時候,不能用int
3. 位數分解的時候,要考慮零的情況。