码迷,mamicode.com
首页 > 其他好文 > 详细

PAT A1005 Spell It Right

时间:2019-08-26 14:55:14      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:参考   style   string   out   turn   sample   col   mes   task   

PAT A1005 Spell It Right

题目描述:

  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

参考代码:

 1 /****************************************************
 2 PAT A1005 Spell It Right
 3 ****************************************************/
 4 #include <iostream>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 const string NUM_WORD[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
10 
11 int main() {
12     int sum = 0;
13     string num;
14     string ans;
15 
16     cin >> num;
17 
18     for (int i = 0; i < num.size(); ++i) {
19         sum += num[i] - 0;
20     }
21 
22     ans = to_string(sum);
23     for (int i = 0; i < ans.size(); ++i) {
24         cout << NUM_WORD[ans[i] - 0];
25         if (i != ans.size() - 1) {
26             cout <<  ;
27         }
28     }
29 
30     return 0;
31 }

注意事项:

  无。

PAT A1005 Spell It Right

标签:参考   style   string   out   turn   sample   col   mes   task   

原文地址:https://www.cnblogs.com/mrdragon/p/11412323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!