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 <stdio.h>
#include <stdlib.h>
int main() {
int a, b, sum, i, n = 1;
char s[1000001];
scanf("%d%d", &a, &b);
sum = a + b;
if (sum == 0)
printf("0");
else if (sum < 0) {
printf("-");
sum = -sum;
}
while (sum) {
s[n++] = sum % 10;
sum = sum / 10;
}
for (i = n - 1; i >= 1; i--) {
if ((i - 1) % 3 == 0 && i != 1)
printf("%d,", s[i]);
else
printf("%d", s[i]);
}
printf("\n");
return 0;
}