1. 程式人生 > >PAT (Advanced Level) 1005 Spell It Right (20 分)

PAT (Advanced Level) 1005 Spell It Right (20 分)

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

Code

#include <iostream>
#include <string>
using namespace
std; string numSpell[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; int main() { string num; cin >> num; int sum = 0; for (int i = 0; i < num.length(); i++) { sum += num[i] - '0'; } int digits[3]; digits[0] = sum / 100; digits[1] = (sum % 100) / 10
; digits[2] = (sum % 10) / 1; if (digits[0]) cout << numSpell[digits[0]] << " "; if (digits[0] || digits[1]) cout << numSpell[digits[1]] << " "; cout << numSpell[digits[2]] << endl; return 0; }

不說了,這道題很簡單。。