您的位置:首页 > 其它

1005. Spell It Right (20)

2018-01-30 14:32 183 查看

1005. Spell It Right (20)

时间限制
400 ms

内存限制
65536 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:
12345

Sample Output:
one five


题目大意:给出两个非负整数,求和后用英文将结果输出

#include <iostream>
#include <cstring>
using namespace std;

int main(){
char data[105];
int index=0;
string num[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int mid=0;
int result[105];

/*for(int i=0; i<10; ++i){
//C++里面使用c的输出方式错误,因为c里面没有字符串
//[Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'
//printf("%s ",num[i].c_str()); //正确
cout<<num[i]<<" ";
}*/

cin>>data;
for(int i=0; i<strlen(data); ++i){ //算出两数之和
mid += (data[i]-'0');
}

while(mid){ //将结果拆分到数组中
result[index++] = mid%10;
mid/=10;
}

if(index!=0){ //若不为 0 则正常输出
for(int i=index-1; i>0; --i){
cout<<num[result[i]]<<" ";
}
cout<<num[result[0]];
}else{ //为 0 的情况
cout<<"zero";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: