PAT 1001. A+B Format (20)

题目地址: http://www.patest.cn/contests/pat-a-practise/1001

我乱搞的, 复习stringstream

 1 #include<cstdio>
 2 #include<sstream>
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 const int SPLITOR = 3;
 8 const string comma = ",";
 9 void print(string s)
10 {
11     int len = s.size();
12     int tmp = len;
13     int r = len % SPLITOR;
14     int index = 0;
15     if (s[index] == '-')
16     {
17         --tmp;
18         cout << s[index++];
19         r = (len - 1) % SPLITOR;
20     }
21     if (r && tmp > SPLITOR)
22     {
23         for (int i = 0; i < r; ++i)
24             cout << s[index++];
25         cout << comma;
26     }
27     int x = 1;
28     for (; index < len; ++index)
29     {
30         cout << s[index];
31         if (!(x++ % SPLITOR) && index < len - 1)
32         {
33             cout << comma;
34         }
35     }
36     cout << endl;
37 
38 }
39 int main()
40 {
41     int a, b, c;
42     while(cin >> a >> b)
43     {
44         c = a + b;
45         stringstream ss;
46         ss << c;
47         string str = ss.str();
48         print(str);
49 
50     }
51     return 0;
52 }

 

posted @ 2015-04-10 02:02  ACSeed  Views(156)  Comments(0)    收藏  举报