1005. Spell It Right (20)
时间限制
400 ms
内存限制
32000 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
/***************************************************88 @ AUTHOR: GAOMINQUAN @ MAIL : ENSOLEILLY@GMAIL.COM @ 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. 位数分解的时候,要考虑零的情况。