分析:将读入的字符转化为数字,直接排个序就可以了。
#include <cstdlib> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int N = 500; int seq[N]; int main() { char c; int t = 0, idx = 0; while ((c = getchar()) != EOF) { if (c == '+' || c == '\n') { seq[idx++] = t; t = 0; } else t = t * 10 + c - '0'; } sort(seq, seq+idx); printf("%d", seq[0]); for (int i = 1; i < idx; ++i) printf("+%d", seq[i]); puts(""); return 0; }