A1005 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

题意:

 给出一个非负数,求出数位之和,并用英语表示这个总和的数位的每一位。

注意:

  1. 对于特殊数据 0,需要直接特判输出zero
  2. 二维数组存储数字对应的英文
  3. 先将数据存于字符数组中,转为 int 型计算sum, 再将sum 依次存于 digit 数组中,依次对应二维数组输出英文
#include <cstdio>
#include <cstring>
char num[10][10] = {
  "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
};
char s[110];
int digit[10];
int main(){
  scanf("%s", s);
  int len = strlen(s);
  int sum = 0, numLen = 0;
  for(int i = 0; i < len; i++){
    sum += (s[i] - '0');
  }
  if(sum == 0){
    printf("%s", num[0]);
  }
  else{
    while(sum != 0){
      digit[numLen++] = sum % 10;
      sum /= 10;
    }
    for(int i = numLen - 1; i >= 0; i--){
      printf("%s", num[digit[i]]);
      if(i != 0)
        printf(" ");
    }
  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_35093872/article/details/86443894