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 9Sample Output
-999,991
这是鄙人的第一个Advanced Level级别的代码C++
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
void format(int data);
template<class in, class out>
out convert(const in& a);
int main()
{
int a,b;
while(cin>>a>>b)
{
format(a + b);
}
return 0;
}
void format(int data)
{
if (data < 0)
{
cout<<"-";
data = data * (-1);
}
string str;
string colloma(",");
str = convert<int, string>(data);
int flag = str.length() % 3;
for(int i = str.length() - 3; i >= flag; i-=3)
{
str.insert(i, colloma);
}
if (str[0] == ',')
{
str = str.substr(1);
}
cout<<str<<endl;
}
template<class in, class out>
out convert(const in& a)
{
stringstream temp;
temp<<a;
out b;
temp>>b;
return b;
}