PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】
2015-08-20 10:09
417 查看
题目链接:http://www.patest.cn/contests/pat-a-practise/1001
题面:
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
Sample Output
题意大意:
输出a+b,以每三位加一个逗号的格式。
解题:
注意后面部分如果小于100需加前缀0。
代码:
题面:
1001. A+B Format (20)
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,以每三位加一个逗号的格式。
解题:
注意后面部分如果小于100需加前缀0。
代码:
#include <cstdio> #include <vector> #include <iostream> #include <string> #include <algorithm> #include <iomanip> using namespace std; int main() { int a,b,ans; cin>>a>>b; ans=a+b; if(ans<0) { cout<<"-"; ans=-ans; } if(ans<1000) cout<<ans<<endl; else if(ans<1000000) cout<<ans/1000<<","<<fixed<<setw(3)<<setfill('0')<<ans%1000<<endl; else cout<<ans/1000000<<","<<fixed<<setw(3)<<setfill('0')<<ans/1000-ans/1000000*1000<<","<<fixed<<setw(3)<<setfill('0')<<ans%1000<<endl; return 0; }
相关文章推荐
- 机房收费之组合查询
- 如何获取Input标签自定义属性的值
- 基于Scala的Actor之上的分布式并发消息驱动框架Akka初体验
- jQuery checkbox的全选与反选
- 关于手机上的 Emoji 图标,这几个背后的故事你应该没听过
- hdu 1599 find the mincost route(floyd 最小环)
- Foundation框架—NSNumber、NSValue、NSDate
- iOS 博客资源精选
- 整数划分模板
- 图像处理中的旋转、平移、缩放——仿射变换的原理和优化
- Java线程状态的转换
- css 背景色渐变
- Effective Objective-C 2.0 第1条:了解Objective-C语言的起源
- easyui编辑器(kindeditor-4.1.10)
- Webkit中HTML5 Video的实现分析-HTMLMediaElement & MediaPlayer
- 解决SVN Can’t open file ‘/XXX/xxx/db/txn-current-lock’错误
- C#中Abstract和Virtual的区别
- Oracle ORA-01940 无法删除当前已连接用户
- 关于python中urllib.urlencode的时候出错:UnicodeEncodeError: ‘ascii’的记录
- 无法ping通