您的位置:首页 > 其它

1005. Spell It Right (20)

2017-11-10 16:00 603 查看
题目:

Given a non-negative integer N, your task is to compute the sum of all the digi
4000
ts 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 <cstdio>
#include <cstring>
using namespace std;

char word[][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

int main()
{
char str[110];
scanf("%s", str);

int len = strlen(str), sum = 0;
for(int i = 0; i < len; i++) //计算出各位之和
sum += str[i] - '0';

int ans[5], k = 0;
while(sum) //将sum的每一位逆序保存在ans数组中
{
ans[k++] = sum % 10;
sum /= 10;
}
printf("%s", word[ans[--k]]);
for(int i = k - 1; i >= 0; i--)
printf(" %s", word[ans[i]]);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: