PAT甲级 1001-A+B Format 【输出格式】

原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400

 

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main() {
 5     int a, b;
 6     cin >> a >> b;
 7     //cout << char(a+48) << endl;
 8     // cout << a + b << endl;
 9     int c = a + b;
10     string str;
11     int d = abs(c);
12     int num = 0;
13     if (d > 1000) {
14         while (d) {
15             int d_1 = d % 10;
16             d = d / 10;
17             str.insert(str.begin(), char(d_1+48));
18             num += 1;
19             if (num == 3 && d != 0) {
20                 str.insert(str.begin(), ',');
21                 num = 0;
22             }
23         }
24         if (c < 0) {
25             str.insert(str.begin(), '-');
26         }
27         cout << str << endl;
28     }
29     else {
30         cout << c << endl;
31     }
32     return 0;
33 }
View Code

 

posted @ 2019-11-15 09:57  esc_coder  阅读(192)  评论(0)    收藏  举报