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
注意:
1. 每三位加一个逗号,是从低位开始数,即1002输出的应该是1,002而非100,2
2. 注意结果可能为负数,要输出负号
3. 和为0的话,直接输出0即可
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> vect;
vector<char> ans_vect;
int main(){
bool flag = false;
int a,b,ans;
cin>>a>>b;
ans = a + b;
if (ans < 0){
flag = 1;
ans = -1 * ans;
}
while(ans){
int t = ans % 10;
vect.push_back(t);
ans /= 10;
}
if (vect.size() == 0){
cout<<0;
return 0;
}
string output = "";
for (int i = 0; i < vect.size(); ++i){
output += char(vect[i] + '0');
if ((i + 1) != vect.size() && (i + 1) % 3 == 0)
output += ',';
}
if (flag) cout<<'-';
reverse(output.begin(),output.end());
cout<<output;
return 0;
}