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 <iostream>
#include <vector>
using namespace std;
int main()
{
int a, b, sum, flag, standard;
vector<char> v1;
while(cin >> a >> b)
{
flag = 0;
sum = a+b;
if(sum < 0)
{
flag = 1;
sum *= -1;
}
if(sum==0)
cout << "0";
standard = 0;
while(sum)
{
standard++;
v1.push_back(sum%10+'0');
if(standard%3==0 && sum/10!=0)
v1.push_back(',');
sum /= 10;
}
if(flag) cout << "-";
for(int i=v1.size()-1; i>=0; i--)
{
cout << v1[i];
}
cout << endl;
}
}
本文介绍了一个简单的程序,用于计算两个整数的和,并将结果以标准格式输出,即使用逗号作为千位分隔符。该程序能够处理-1,000,000到1,000,000范围内的整数。
1565

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



