1001. A+B Format (20)
2016-02-08 10:25
302 查看
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
Sample Output
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
#include <iostream> #include <string> #include <stdio.h> using namespace std; int main() { int a,b; cin>>a>>b; char str[20]; sprintf(str,"%d",a+b); string raw(str); string output; int count=0; for(int i=raw.size() - 1;i>=0;i--,count++) { if(count != 0 &&count % 3 == 0 && raw[i] != '-') output.insert(output.begin(),','); output.insert(output.begin(),raw[i]); } cout<<output<<endl; return 0; }
方法2:
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main() { int a,b; cin>>a>>b; if(a+b<0)cout<<'-'; char res[30]; sprintf(res,"%d",a+b>0?a+b:-(a+b)); string output(res); int chop = output.size()%3; for(int i=0;i<output.size();i++) { if( (i-chop)%3 == 0 && i != 0) cout<<','; cout<<output[i]; } cout<<endl; system("pause"); return 0; }
相关文章推荐
- libdvbpsi源码分析(四)PAT表解析/重建
- PAT配置
- 什么是端口复用动态地址转换(PAT) 介绍配置实例
- MikroTik layer7-protocol
- PAT是如何工作的
- PAT 乙级题:1002. 写出这个数 (20)
- PAT (Advanced Level) Practise 1001-1010
- 数据结构学习与实验指导(一)
- PAT Basic Level 1001-1010解题报告
- 1001. 害死人不偿命的(3n+1)猜想
- 1002. 写出这个数
- 1032. 挖掘机技术哪家强
- 1001. 害死人不偿命的(3n+1)猜想 (PAT basic)
- 1002. 写出这个数(PAT Basic)
- 1004. 成绩排名(PAT Basic)
- 1006. 换个格式输出整数(PAT Basic)
- 1007. 素数对猜想(PAT Basic)
- 1008. 数组元素循环右移问题
- 1009. 说反话(PAT Basic)
- 1011. A+B和C(PAT Basic)