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
思路:把sum转换成string,然后每隔3位加一个“,”;
这里用了两个函数,一个是上面的那个,一个是string类型的insert()
#include<iostream>
#include <string>
#include <strstream>
using namespace std;
int main()
{
int a,b;
strstream ss;
string s;
cin>>a>>b;
int sum=a+b;
ss<<sum;
ss>>s;
int len=s.length();
if(s[0]=='-')
{
for(int i=len-3;i>1;i=i-3)
{
s.insert(i,",");
}
}
else{
for(int i=len-3;i>0;i=i-3)
{
s.insert(i,",");
}
}
cout<<s<<endl;
}