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
思路:将a+b的和存入char型数组c[N],再输出
#include<stdio.h>
int Trans(int n,char c[]);
int main(){
int a,b,len,sum,i,r,count=0;
char c[10];
scanf("%d %d",&a,&b);
sum=a+b;
len=Trans(sum,c);
if(sum<0){printf("-");sum=-sum;}
r=len%3;
for(i=len-1;i>=0;i--){
printf("%c",c[i]);
if(r>0){
r--;
if(r==0&&len>=3)printf(",");
continue;
}
count++;
if(count%3==0&&i!=0)printf(",");
}
}
int Trans(int n,char c[]){
int i=0,m;
m=n<0?(-n):n;
while(m!=0){
c[i++]=m%10+'0';
m/=10;
}
if(n==0){
c[0]='0';
i=1;
}
return i;
}