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
#include <iostream>
#include<string>
#include <algorithm>
#include <vector>
#include <stdio.h>
using namespace std;
int main(void)
{
vector<string> myvector;
vector<string>::iterator it;
int a,b,i=1,flag=0;
int sum[9];
char temp[2];
cin>>a>>b;
int s=a+b;
if(s==0)
{
cout<<"0";
return 0;
}
if(s<0)
{
s=-s;
flag=1;
}
for(; s!=0; s/=10)
{
sum[i]=s%10;
i++;
}
int k=0;
if(flag==1)cout<<"-";
for(int j=1; j<i; ++j)
{
// itoa(sum[j],temp,10);两种方式integer转string
sprintf(temp,"%d",sum[j]);
myvector.push_back(temp);
k++;
if(k==3&&j<i-1)
{
myvector.push_back(",");
k=0;
}
}
reverse(myvector.begin(),myvector.end());
for(it=myvector.begin(); it!=myvector.end(); ++it)
cout<<*it;
}