PAT程序设计甲级1005
题目
1005 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 (≤10
100
).
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
题目意思
给你个100位的数字,求各个位数相加的和的英文。
思路
1、数字的位数大到100位,一开始读题时没考虑这个问题,而longlong只有18位,所以只能用字符串去读。
2、计算各个位数之和。
3、将所得的和分开用英文表示。这个就首先要用字符数组并且是二维数组先存。
代码如下
javascript
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
char hx[103];
scanf("%s",hx);
int s = strlen(hx);
int sum =0;
for(int i=0 ;i<s;i++)
{
sum = sum +hx[i]-'0';
}
char mz[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int a[10],i=0;
while(sum)
{
a[i++]=sum%10;
sum=sum/10;
}
for(int j=i-1;j>0;j--)
{
printf("%s ",mz[a[j]]);
}
printf("%s",mz[a[0]]);
return 0;
}