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 <cstdio>
# include <cmath>
# include <cstdlib>
int main(){
int a, b, sum, c[5], i = 0;
scanf("%d %d", &a, &b);
sum = a + b;
if(!sum){
printf("0");
return 0;
}
if(sum < 0){
printf("-");
sum = -sum;
}
while(sum){
c[i++] = sum % 1000;
sum /= 1000;
}
printf("%d", c[--i]);
while(i--){
printf(",%03d", c[i]);
}
return 0;
}
格式化输出加法结果
本文介绍了一个C语言程序,该程序接收两个整数输入,计算它们的和,并以标准格式输出结果,其中数字每三位用逗号分隔。
232

被折叠的 条评论
为什么被折叠?



