PAT A1005 Spell It Right
Posted mrdragon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT A1005 Spell It Right相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
1005. Spell It Right(20)—PAT 甲级