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 Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.
Output Specification:
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并以标准格式输出总和-也就是说,必须用逗号将数字分成三组(除非少于四位)。
输入规格:
每个输入文件包含一个测试用例。 每种情况都包含一对整数a和b,其中-10‐6‐≤a,b≤10‐6。 这些数字用空格分隔。
输出规格:
对于每个测试用例,应在一行中输出a和b的总和。 总和必须以标准格式书写。
代码:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
using namespace std;
int main()
{
int a,b;
string ans;
cin>>a>>b;
int c=a+b;
if(c==0)
{
cout<<c;
return 0;
}
if(c<0) cout<<"-";
c = abs(c);
while(c) //把c转为字符串ans
{
ans+=c%10+'0';
c/=10;
}
for(int i=ans.size()-1;i>=0;i--) //倒着输出
{
cout<<ans[i];
if(i%3==0 && i!=0)
cout<<",";
}
return 0;
}