准备每天刷两题PAT真题

1001 A+B Format 

模拟输出,注意格式

PAT 甲级真题题解(1-62)
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 string ans = "";
 8 
 9 int main() {
10     int a, b, c, cnt = 0;
11     cin >> a >> b;
12     c = a + b;
13     if(c == 0) {
14         cout << 0 << endl;
15         return 0;
16     }
17     while(c) {
18         if(cnt % 3 == 0 && cnt != 0) ans = ans + ",";
19         char ch = abs(c % 10) + '0';
20         ans = ans + ch;
21         c /= 10;
22         cnt++;
23     }
24     reverse(ans.begin(), ans.end());
25     if(a + b < 0) ans = "-" + ans ;
26     cout << ans << endl;
27     return 0;
28 }
View Code

相关文章: