Pat(Advanced Level)Practice--1005(Spell It Right)

Pat1005代码

题目描述:

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

AC代码:
#include<cstdio>
#include<cstring>
#define MAX 110

using namespace std;

int main(int argc,char *argv[])
{
	int i,j,len;
	int sum=0,index;
	int stack[MAX];
	char str[MAX];
	char mode[10][10]={"zero","one","two","three","four",
	"five","six","seven","eight","nine"};
	scanf("%s",str);
	len=strlen(str);
	for(i=0;i<len;i++)
		sum=sum+(str[i]-'0');
	j=0;
	while(sum)
	{
		stack[j++]=sum%10;
		sum=sum/10;
	}
	index=stack[--j];
	printf("%s",mode[index]);
	--j;
	for(;j>=0;j--)
	{
		index=stack[j];
		printf(" %s",mode[index]);
	}
	printf("\n");

	return 0;
}


你可能感兴趣的:(Algorithm,数据结构,C++,pat,advance)