题目
005 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 (≤ 10 100 10^{100} 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
思路
这是个大数处理题,string读入,计算,用栈输出即可,时间也比较宽,cout也足够
用printf输出string printf("%s",string.c_str());
stl 中stack ,弹栈需要先top(),获取栈顶元素后,再弹栈pop()。pop无返回值。top不删除元素。
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main(){
string str;
//scanf("%s",&str);
getline(cin,str);
int i,l = str.size() ,mod=0 ;
long long sum = 0;
stack<string> s;
for(i=0;i<l;i++){
sum += str[i]-'0';
}
if(sum){
while(sum){
mod = sum %10;
switch(mod){
case 1:
s.push("one");
break;
case 2:
s.push("two");
break;
case 3:
s.push("three");
break;
case 4:
s.push("four");
break;
case 5:
s.push("five");
break;
case 6:
s.push("six");
break;
case 7:
s.push("seven");
break;
case 8:
s.push("eight");
break;
case 9:
s.push("nine");
break;
case 0:
s.push("zero");
break;
}
sum /= 10;
}
}else {
cout<<"zero";
return 0;
}
printf("%s",s.top().c_str());
//cout<<s.top();
s.pop();
while(!s.empty()){
//cout<<" "<<s.top();
printf(" %s",s.top().c_str());
s.pop();
}
return 0;
}