Spell It Right (20)
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
题目大意:给一个数n,n<=10e100 ,计算n的各位数字之和 并以英文的形式输出
分析:PAT评测不能用itoa()函数,然后注意一下输入为0时的情况即可
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
char snum[110];
char s[10][6]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int sum=0;//存放所有数字之和
int tem[10];//存放和sum的每位数字,方便输出时转换为英文
scanf("%s",snum);
int lens=strlen(snum);//输入数据的长度
for(int i=0;i<lens;++i){//输入数据每位数字加和
sum+=snum[i]-'0';//因为输入时以字符串形式 所以这里需要-'0'才能获得真实值
}
if(sum==0){//加和为0直接输出zero即可
printf("%s",s[0]);
return 0;//让主函数在这里直接返回 不再向下执行
}
int i=0;
while(sum){//求sum的每位数字 因为输出是英文的每位数字
tem[i++]=sum%10;
sum=sum/10;
}
i--;//因为上面有i++ 所以此时tem[i]里没有值 tem[i-1]里才是第一位数字
for(;i>=0;i--){
if(i==0)//控制输出格式 第一位数字直接输出
printf("%s",s[tem[i]]);//因为tem数组里放的是值 所以要输出s[tem[i]]
else//之后每次输出 前面加空格 这样最后一位输出后不会有多余的空格
printf("%s ",s[tem[i]]);
}
return 0;
}