Codeforces Round #197 (Div. 2) A. Helpful Maths

http://codeforces.com/problemset/problem/339/A

题意:
把一个求和的式子(只包含+1,+2,+3)重新排序为递增的求和式子。

比如输入3+2+1,输出1+2+3。**

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;

int main() {
string s, out = "";
int n1 = 0, n2 = 0, n3 = 0;
cin >> s;
int n = s.length();
for (int i = 0; i < n; i++) {
if (s[i] == '1') n1++;
else if (s[i] == '2') n2++;
else if (s[i] == '3') n3++;
}
for (int i = 0; i < n1; i++) out += "1+";
for (int i = 0; i < n2; i++) out += "2+";
for (int i = 0; i < n3; i++) out += "3+";
if (out[out.size() - 1] == '+') {
for (int i; i < out.size() - 1; i++) cout << out[i]; // 删去最后的“+”
}
return 0;
};