注意几种情况:
a+b = 0
a+b = -10或-100或-1000
a+b = 1000或10000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a, b, sum, group;
int i, len, count = 0;
char str[10];
scanf("%d %d", &a, &b);
sum = a + b;
sprintf(str, "%d", sum);
len = strlen(str);
if(str[0] == '-' )
{
group = (len - 1) % 3;
printf("-");
for(i = 1; i <= group; i++)
{
printf("%c", str[i]);
}
}
else
{
group = len % 3;
for(i = 0; i < group; i++)
{
printf("%c", str[i]);
}
}
if(group != 0 && group != 1)
{
printf(",");
}
for(; i < len; i++)
{
printf("%c", str[i]);
count++;
if(count == 3 && i != len-1)
{
printf(",");
count = 0;
}
}
printf("\n");
return 0;
}