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 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
#include<bits/stdc++.h>
using namespace std;
string out(int num);
int main()
{
int a,b;
while(cin>>a>>b)
{
cout<<out(a+b)<<endl;
}
return 0;
}
string out(int num)
{
char number[]={'0','1','2','3','4','5','6','7','8','9'};
string ret,s;
int count;
ret="";
s="";
count=0;
if(num<0)//注意结果小于零的情况
{
s="-";
num=0-num;
}
if(num==0)//注意结果为零的情况
{
return "0";
}
while(num)
{
int temp;
temp=num%10;
count++;
num=num/10;
if(count==3&&num!=0)//注意刚好是三个数的情况
{
ret=number[temp]+ret;
ret=","+ret;
count=0;
}
else
{
ret=number[temp]+ret;
}
}
return s+ret;
}