您的位置:首页 > 其它

1005. Spell It Right (20)

2015-11-21 19:27 288 查看
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<cstdio>
#include<cstring>
#include<stack>
using namespace std;

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

stack<int> result;
char input[102];

int main(void)
{
scanf("%s",input);
int sum = 0;
int length = strlen(input);
for(int i = 0;i<length;++i)
{
sum+=input[i] - '0';
}
do
{
result.push(sum%10);
sum /= 10;
}while(sum>0);
do
{
printf("%s",digit[result.top()]);
result.pop();
if(!result.empty())
printf(" ");
}while(!result.empty());

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