PAT 甲级 1001 A+B Format
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400
题意
给出两个数,将二者的和从个位起以三位为间隔输出。
代码
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; string s = to_string(a + b); int cnt = 0; for (int i = s.size() - 1; i >= 0; i--) { if (++cnt % 3 == 0) { if (i - 1 >= 0 and isdigit(s[i - 1])) { s.insert(i, ","); } } } cout << s << "\n"; }

浙公网安备 33010602011771号