关闭

1005. Spell It Right (20)

71人阅读 评论(0) 收藏 举报
分类:

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;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13436次
    • 积分:1087
    • 等级:
    • 排名:千里之外
    • 原创:102篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    最新评论