您的位置:首页 > 理论基础 > 数据结构算法

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

2014-02-09 22:40 369 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息