分析
题目大意是求a+b,然后按照从后往前每三位数字前田一个“,”,需要注意的是最前面如果没有数字或者最前面是“-”,不能添加“,”。
code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
void solve() {
int a, b;
cin >> a >> b;
int c = a + b;
string s = to_string(c);
int cnt = 0;
string res = "";
if (c < 0) {
for (int i = s.size() - 1; i > 0; i -- ) {
cnt ++ ;
res += s[i];
if (cnt % 3 == 0 && i != 1) {
res += ',';
}
}
res += '-';
} else {
for (int i = s.size() - 1; i >= 0; i -- ) {
cnt ++ ;
res += s[i];
if (cnt % 3 == 0 && i != 0) {
res += ',';
}
}
}
reverse(res.begin(), res.end());
cout << res << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int _ = 1;
// cin >> _;
while (_ -- ) {
solve();
}
return 0;
}