1001. A+B Format (20)-PAT甲级真题
2016-07-21 19:25
627 查看
Calculate
a + b and output the sum in standard format — that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input
Each
input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
For
each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample
Input
-1000000
9
Sample
Output
-999,991
a + b and output the sum in standard format — that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input
Each
input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
For
each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample
Input
-1000000
9
Sample
Output
-999,991
#include <iostream> using namespace std; int main() { int a; int b; cin >> a >> b; int result = a + b; if (result < 0) { cout << "-"; result = result * (-1); } int s[7]; if (result == 0) { cout << 0; return 0; } int i = 0; while (result != 0) { s[i++] = result % 10; result = result / 10; } int count = i; for (int j = i - 1; j >= 0; j--) { cout << s[j]; count--; if (count % 3 == 0 && count != 0) cout << ","; } return 0; }
相关文章推荐
- 更新win7资源管理器
- 转自大楚网:微软SAPI:让你的软件能说会道
- JavaScript内存和性能
- Android Classloader热修复技术之百家齐放
- 实习用到的hive 相关sql
- MxNet教程:使用一台机器训练1400万张图片
- 慢启动
- 将搜索二叉树转换成有序的双向链表
- PHP Reflection API是PHP5才有的新功能
- 1002. A+B for Polynomials (25)-PAT甲级真题
- 5步实现Floating Action Button
- 1100. Mars Numbers (20)-PAT甲级真题
- HDU 5738 Eureka 统计共线的子集个数
- (转) 使用Speech SDK 5.1文字转音频
- Proximal Algorithms--proximal gradient algorithm
- 常见排序java实现
- TCP长连接与短连接的区别
- [LeetCode] 45. Jump Game II
- http post和get的区别
- xcode装好了的插件好像没安装一样