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 −. 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
大意:
计算 + 并以标准格式输出总和-也就是说,必须用逗号将数字分成三组(除非少于四位)。
输入规格:
每个输入文件包含一个测试用例。每个案例包含一对整数一种 和 b 哪里 −。这些数字用空格分隔。
输出规格:
对于每个测试用例,您应该输出 一种 和 b在一排。总和必须以标准格式书写。
输入样例:
-1000000 9
样本输出:
-999,991
#include<iostream> #include<algorithm> #include<cstdlib> #include<cstdio> #include<queue> #include<stack> #include<cstring> #include<string.h> #include<cmath> using namespace std; const int maxn=1010; int main(){ int a,b; scanf("%d %d",&a,&b); int c=a+b; c=abs(c); int d[maxn]; int i=0; while(c){ d[i]=c%10; c/=10; i++; } if(a+b<0){ printf("-"); } if(a+b==0){ printf("0 "); return 0; } for(int j=i-1;j>=0;j--){ printf("%d",d[j]); if((j)%3==0&&j>0){ printf(","); } } printf(" "); return 0; }