1. 程式人生 > >Pat1005:Spell It Right

Pat1005:Spell It Right

ase log spec numbers ble xtra tro name value

1005. Spell It Right (20)

時間限制 400 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者 CHEN, Yue

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 (<= 10100

).

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

思路
1.10^100位數字,用字符串輸入就行。
2.用vector保存下每個數字對應的單詞
2.因為題目原因,實際最大也就101個數字加起來,最大不會超過909,所以可以用int保存數字。
3.int轉為字符串,輸出串每一位數字對應的單詞就行。
代碼
#include<iostream>
#include<vector>
using namespace std;
vector<string> numbers =
{
  "zero","one","two","three","four","five","six","seven","eight","nine"
};
int main()
{
   string n;
   while(cin >> n)
   {
       int sum = 0;
       for(int i = 0;i < n.size();i++)
       {
          sum 
+= (n[i] - 0); } string s = to_string(sum); cout << numbers[s[0] - 0]; for(int i = 1; i < s.size();i++) { cout << " " << numbers[s[i] - 0]; } } }

 

Pat1005:Spell It Right