PAT甲级 1005 Spell It Right (20 分) 题解 递归

1005 Spell It Right (20 分)
Time limit: 400 ms
Memory limit: 64 MB
Source limit: 16 KB
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

Each input file contains one test case. Each case occupies one line which contains an N   ( ≤ 1 0 100 ) N\ (≤10^{100}) N (10100).

Output

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.

Example

input
12345
output
one five

思路:

getchar逐个逐个字符读入数字,利用sum变量保存所有数字之和。最后利用递归来输出sum转换为英文的结果。

参考代码:

#include 
const char* s[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
void print(int N) {
    if (N > 10) print(N / 10);
    printf("%s%s", N != N % 10 ? " " : "", s[N % 10]);
}
int main() {
    int ch, sum = 0;
    while ((ch = getchar()) != '\n')
        sum += ch - '0';
    print(sum);
    return 0;
}

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