1. 程式人生 > >PAT (Advanced Level) Practice 1005 Spell It Right (20 分)(C++)(甲級)

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

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; }