1001. A+B Format (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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<stdio.h> #include<string.h> #include<math.h> using namespace std; int main() { int n, m; scanf("%d%d", &n, &m); int sum = n + m; int a[10]; memset(a, 0, sizeof(a)); int flag = 0; int cnt = 0; if(sum < 0) flag = 1; sum = fabs(sum); if(sum == 0) printf("%d\n",sum); while(sum) { a[cnt++] = sum%10; sum/=10; } if(flag == 1) printf("-"); for(int i = cnt - 1; i >= 0; i--) { printf("%d",a[i]); if(i == 6 || i == 3) printf(","); } }
本文介绍了一个简单的程序设计问题:如何将两个整数相加的结果以标准格式输出,即每三位数字用逗号分隔。文章详细展示了输入、输出的要求,并提供了一段完整的C++代码实现。

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



