题目:
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 9Sample Output
-999,991
#include<cstdio> using namespace std; int main() { int a, b, c, d[5], i, flag=1, num=0; scanf ("%d %d", &a, &b); c = a + b; if (c < 0) { flag = -1; c = -c; } if (c == 0) printf ("0"); while (c != 0) { d[num++] = c % 1000; c /= 1000; } if (flag == -1) printf ("-"); if (num > 0) printf ("%d", d[num-1]); for (i=num-2; i>=0; i--) printf (",%03d", d[i]); return 0; }
本文介绍了一个简单的C++程序,该程序接收两个整数输入,计算它们的和,并以标准格式输出结果,其中涉及如何正确使用逗号分隔千位数的方法。
1567

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



