1005. Spell It Right (20) 数字之和转英文输出
2017-03-08 17:33
330 查看
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
题目比较简单,唯一要注意的是对于sum==0这种情况的特殊处理,编程很多时候都有特殊情况,代码的完整性很重要,编程时除了要考虑基本功能,还应该注意边界处理,特殊情况,非法输入之类的情况。
另外,要多用vector,相比于数组它不需要初始化内存长度,很方便。
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
题目比较简单,唯一要注意的是对于sum==0这种情况的特殊处理,编程很多时候都有特殊情况,代码的完整性很重要,编程时除了要考虑基本功能,还应该注意边界处理,特殊情况,非法输入之类的情况。
另外,要多用vector,相比于数组它不需要初始化内存长度,很方便。
#include <iostream> #include <string> #include <vector> using namespace std; int main(void) { string English[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; string input; vector<int> sumNum; cin >> input; int sum = 0; for (int i = 0; i < input.length(); i++) { sum += (int)(input[i] - '0'); } int j = 0; if (sum == 0) cout << English[0]; while (sum) { sumNum.push_back(sum % 10); ++j; sum /= 10; } while (j) { if (j>1) cout << English[sumNum[j - 1]] << " "; else cout << English[sumNum[j - 1]]; --j; } system("pause"); return 0; }
相关文章推荐
- [PAT甲级]1005. Spell It Right (20)(求数字各个位上的和,英文输出)
- 1005. Spell It Right (20)
- [PAT]1005. Spell It Right (20)@Java
- 浙江大学PAT上机题解析之1005. Spell It Right (20)
- PAT(甲级)-1005. Spell It Right (20)
- 1005. Spell It Right (20)
- PAT:A1005. Spell It Right (20/20)
- 1005. Spell It Right (20)
- Spell it Right(20) 1005
- PAT - 甲级 - 1005. Spell It Right (20)
- 1005. Spell It Right (20)
- 1005. Spell It Right (20)水题
- 1005. Spell It Right (20)
- Python 实现Pat 1005. Spell It Right (20)
- 1005. Spell It Right (20)
- 1005. Spell It Right (20)
- 1005.Spell It Right (20)
- 1005. Spell It Right (20)
- 【PAT (Advanced Level)】1005. Spell It Right (20)
- PAT1005. Spell It Right (20)