PAT (Advanced Level) Practice 1005 Spell It Right (20 分)(C++)(甲级)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37454852/article/details/85869962

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


#include <cstdio>
#include <cstring>
#include <cmath>

int main()
{
    char N[110] = {0};
    int sum = 0;
    char ans[11][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "\0"};
    int S[10] = {0};//辅助栈,用来存储sum的拆开后的各位数字
    int top = 0;
    scanf("%s", N);
    int len = strlen(N);
    for(int i=0; i<len; i++) sum += N[i] - '0';
    do
    {
        S[top++] = sum%10;
        sum /= 10;
    }while(sum);
    printf("%s", ans[S[--top]]);//打印栈,注意控制输出格式
    while(top>0) printf(" %s", ans[S[--top]]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/85869962
今日推荐