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
Process
这道题最大的障碍是语言不通,不过还好可以看样例猜题意~
Code
#include<bits/stdc++.h>
using namespace std;
long long a, b;
stack<int> c;
int main()
{
cin >> a >> b;
long long temp = a + b;
if (temp < 0)
cout << "-";
temp = fabs(temp);
if (temp == 0)
cout << "0";
else
{
while (temp >= 1)
{
c.push(temp % 1000);
temp /= 1000;
}
int flag = 0;
while (!c.empty())
{
if (flag == 0)
printf("%d", c.top());
else
printf("%03d", c.top());
c.pop();
cout << (c.empty() ? "\n" : ",");
flag++;
}
}
return 0;
}