1001 A+B Format (20)(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<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
char s[];
int main()
{
int a,b;
cin>>a>>b;
a=a+b;
int start=;
int temp,len=;
b=a;
if(a<)a=-a;
if(a==)cout<<;
while(a){
temp=a%;
a/=; len++;
if(len>&&len%==)
s[start++]=',';
s[start++]=temp+'';
}
if(b<)
cout<<'-'; for(int i=start-;i>=;i--)
cout<<s[i];
return ;
}
//就是求a+b的和,并且输出。非常简单。并且和都不会溢出int.第一次提交的时候只得了19分,发现是因为在输入两个0时,程序没有输出。之后对两者和进行判断,若和为0,直接输出0即可。