A-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 (≤10
100
).
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 <string>
#include <iostream>
using namespace std;
int main(){
int i,j,w[100]={},sum=0;
string str;
string word[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
cin >> str;
for(i=0;str[i]!=NULL;i++){//计算每一位数的和
sum+=(str[i]-'0');
}
if(sum==0){//如果为0直接输出zero
cout << "zero";
return 0;
}
for(j=0;sum>0;j++){//sum的每一位拆分存入数组中
w[j]=sum%10;
sum/=10;
}
for(i=j-1;i>=0;i--){//输出
cout << word[w[i]];
if(i>0)cout << ' ';
}
return 0;
}
本文介绍了一个简单的程序,该程序接收一个非负整数N作为输入,计算其各位数字之和,并将这个和的每个数字用英文单词输出。文章提供了完整的C++代码实现,并附带了输入输出示例。
405

被折叠的 条评论
为什么被折叠?



