版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37454852/article/details/85554680
1001 A+B Format (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 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 <cstdio>
#include <cstring>
#include <cmath>
int main()
{
int a = 0, b = 0;
scanf("%d %d", &a, &b);
int sum = a + b;
int S[5] = { 0 };//辅助栈
int top = -1;//栈顶指针
if (!sum) { printf("0"); return 0; }//和为0直接输出了
if (sum < 0) { printf("-"); sum = -sum; }//和为负数先输出符号,之后正负格式统一
while (sum)
{
S[++top] = sum % 1000;
sum /= 1000;
}
printf("%d", S[top--]);//第一个逗号之前不需要补零
while (top >= 0) printf(",%03d", S[top--]);//注意输出格式
return 0;
}