1001. A+B Format (20)
来源:互联网 发布:淘宝开的店铺怎么关闭 编辑:程序博客网 时间:2024/05/16 12:24
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 9Sample Output
-999,991
PAT的OJ服务器是64位的Linux,很多情况下都可不必担心int的溢出问题,可以不用考虑整型字符串加法的问题。
本题的难点在于分隔符添加的问题。
#include <iostream>#include <stack>#include <string>#include <cstdlib>using namespace std;int main(){long int a,b;long int sum;int minusflag = 0;stack<char> s;cin >> a >> b;sum = a + b;if(sum < 0) {minusflag = 1;sum = -sum;}int cnt = 0;if(sum == 0)s.push('0');while(sum != 0){s.push(sum % 10 + '0');cnt++;if(cnt == 3){s.push(',');cnt = 0;}sum /= 10;}if(s.top() == ',') s.pop();if(minusflag == 1)cout << '-';while(!s.empty()){char tmp = s.top();cout << tmp;s.pop();}system("pause");return 0;}
0 0
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 洛谷2448 无尽的生命
- 解决输入vi不等于vim的问题
- UVA165连续邮资问题
- 原串翻转
- HDOJ 2036 求多边形面积
- 1001. A+B Format (20)
- Hibernate getCurrentSession() 方法的细节
- 金融系统中PBOC/EMV的TLV的算法实现(含C++/C#)
- 一些索引的限制
- 页面置换算法
- 将110号员工的工资修改为其所在部门的平均工资多100元
- iOS 中获得键盘的弹出和隐藏
- 第4周—项目3.(3)单链表递增
- 安卓控件使用系列18:DatePicker日期控件和TimePicker时间控件的使用