1005. Spell It Right (20) 数字之和转英文输出
来源:互联网 发布:认字软件 编辑:程序博客网 时间:2024/06/10 20:26
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,相比于数组它不需要初始化内存长度,很方便。
#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;}
0 0
- 1005. Spell It Right (20) 数字之和转英文输出
- [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)
- 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)
- 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)
- 1005. Spell It Right (20)
- 1005. Spell It Right (20)
- 利用maven-compiler-plugin插件将maven工程打成jar包
- iOS 视频的录制、合成以及播放
- vim configure
- 学习java应该如何理解反射?
- Windows驱动开发:2 调试环境搭建
- 1005. Spell It Right (20) 数字之和转英文输出
- springboot集成log4j(完成错误信息邮件发送)
- HZNU Training 3—M
- 使用StarUML 画类图
- ASP.NET ZERO 学习 —— (13) 应用开发Demo之页面呈现Person数据
- Unity3D 如何复制内容到剪切板
- 详解post和get请求
- bootstrap去掉表格边框
- 游动的鱼AS3