您的位置:首页 > 其它

PAT A 1005. Spell It Right (20)

2017-02-20 20:31 453 查看
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
// 题目分析:
字符串数组存储字符串常量
// 代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
char a[110];
string strs[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
scanf("%s",a);
int i=0;
int sum=0;
while(a[i]!='\0'){
sum+=a[i]-'0';
i++;
}
vector<int> v;
while(sum!=0){
v.push_back(sum%10);
sum/=10;
}
for(int i=v.size()-1;i>0;i--){
cout<<strs[v[i]]<<' ';
}
if(v.empty()){
cout<<"zero";
}else{
cout<<strs[v[0]];
}
system("pause");
return 0;
}
//结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT