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
个人思路
水题略过
代码实现
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define ep 1e-2
using namespace std;
const int maxn = 15;
int main() {
int a, b;
cin >> a >> b;
int sum = abs(a + b), cnt = 0;
string sum_str = "";
if (a + b == 0) {
sum_str += '0';
}
else {
while (sum != 0) {
cnt ++;
sum_str = char((sum%10)+'0') + sum_str;
sum /= 10;
if (cnt % 3 == 0 && sum != 0)
sum_str = ',' + sum_str;
}
if (a + b < 0)
sum_str = '-' + sum_str;
}
cout << sum_str << endl;
return 0;
}
总结
学习不息,继续加油