题目描述:
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
我的思路:
首先计算各个数位的和,然后将各个数位(从低位开始)对应的字符串存放在vector数组中,用reverse_iterator迭代器进行逆向输出即可
我的代码:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string numbers[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
vector<string>answer;
int main(){
string s;
cin>>s;
int sum=0;
for(int i=0;i<s.size();i++){//求各数位之和
sum+=s[i]-'0';
}
if(sum==0){
cout<<"zero";
return 0;
}
while(sum!=0){
int temp=sum%10;
answer.push_back(numbers[temp]);
sum/=10;
}
vector<string>::reverse_iterator it;
for(it=answer.rbegin();it!=answer.rend();it++){
cout<<*it;
if(it!=answer.rend()-1){
cout<<" ";
}
}
return 0;
}