思路:
(1)本题是实现a+b然后再进行标准输出,
(2)如果结果是负的就先输出负号,然后结果取相反数
(3)然后再分离出来每一位,每三位中间加一个逗号
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<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{
long long a,b,sum;
int ans[100];
cin>>a>>b;
sum = a+b;
if(sum<0){
cout<<'-';
sum = sum*(-1);
}
int cnt=0,count=0;
do{
ans[cnt++] = sum%10;
sum /=10;
count++;
if(count%3==0){
ans[cnt++]=-1;
}
}while(sum!=0);
if(ans[cnt-1]==-1){
cnt--;
}
for(int i=cnt-1;i>=0;i--){
if(ans[i]==-1){
cout<<',';
}
else{
cout<<ans[i];
}
}
return 0;
}