PAT-甲级-1005 Spell It Right

1005 Spell It Right (20)(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

 

题目分析

给一个无符号数字N(范围是0~10^100),计算每一位数字相加的和,将结果的每一位数字用英语表示出来

 

思路

将英语数字存在字符串数组里,下标和值对应。然后输入的数字存到字符数组里,根据ASCII码得到数字值,得到和。

由于N最多100位,每位最大是9,即和最大为900,三位数。所以通过取整取余获得和每位的数字。作为下标获取英语单词。

这个题蛮简单的还,主要要注意的地方一个是字符数字结束的条件,一个是ASCII码。

 

代码

#include
#include
#include 
#include
#include 
using namespace std;
int main(){
    string eng[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    char str[100];
    int i,num;
    int sum = 0;
    scanf("%s",str);
    for(i = 0;str[i]!='\0';i++){
        sum+=str[i]-'0';
    }
 //   cout<

 

你可能感兴趣的:(PAT甲级)