C语言编程100题-9.7

9.7
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 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
样例输入:
12345
样例输出:
one five

#include<stdio.h>
#include<math.h>
int main()
{
       int x, k, i, j, I, N = 0;//输入x,x的位数是k,各数位上数字之和为N
       int s[10000];
       scanf("%d", &x);
       for (k = 0; x / pow(10, k) >= 1; k++)//求出x的位数k(常用)
              ;
       for (i = 0; i <= k - 1; i++)
       {
              I = pow(10, i);
              s[i] = (x/ I) % 10;//取出各个数位上的数字,从个位开始(常用),并求和
              N = s[i] + N;
       }
       int m,n;int t[10000];//N的位数是m
       for (m = 0; N/ pow(10, m) >= 1; m++)//求出N的位数m
              ;
       for (n=0; n <m - 1; n++)
       {
              I = pow(10, m-n-1);
              t[n] = (N/ I) % 10;//取出各个数位上的数字(除个位外),以十位结束(常用)
              switch (t[n])
              {
              case 1:printf("one "); break;
              case 2:printf("two "); break;
              case 3:printf("three "); break;
              case 4:printf("four "); break;
              case 5:printf("five "); break;
              case 6:printf("six "); break;
              case 7:printf("seven "); break;
              case 8:printf("eight "); break;
              case 9:printf("nine "); break;
              default:printf("zero ");//也可以写为:case 0:printf("zero "); break;
              }
              t[m - 1] = N % 10;//取出N的个位
              switch (t[m-1])
              {
              case 1:printf("one"); break;
              case 2:printf("two"); break;
              case 3:printf("three"); break;
              case 4:printf("four"); break;
              case 5:printf("five"); break;
              case 6:printf("six"); break;
              case 7:printf("seven"); break;
              case 8:printf("eight"); break;
              case 9:printf("nine"); break;
              default:printf("zero");
              }
       }
       system("pause");
       return 0;
}

猜你喜欢

转载自blog.csdn.net/nollysoul/article/details/89785742