Getting Exercise 5 string useful problem math problem

Written for: http://codeforces.com/problemset/problem/339/A
but on the basis of the original title did some strengthening in order to strengthen students' ability to handle input.

Title Description

Cong Cong is a third-year student Alice magic school. He is now learning addition.
The teacher to write a set of summation. Cong Cong need to calculate the results of this series of operation.
Teacher problem is the addition (data range of integers between 1 and 1000) comprising a plurality of integers, and a character '+' between two positive numbers.
However, if after the previous element than the presence of a large element of the equation, Cong Cong will not forget.
Cong Cong adding a formula can be solved, if and only if each of the two adjacent elements are in the addition operation condition "is not larger than a previous element after the element".
For example, can be calculated Congcong "1 + 2 + 3 + 4 + 5" and "2 + 2 + 3";
however, not possible to calculate Cong Cong "3 + 2 + 5", because in this formula 2 and 3-phase o 2 and 3 in front of, but 3 is larger than 2.
Now the teacher has arranged the addition formula down, in order to enable Cong Cong calculate this problem, please help swap the order of addition of the element, so that this formula can be calculated Cong Cong and.

Input Format

Input line contains a non-empty string s (length s is not more than 1,000), it is needed for additive formulated Cong Cong calculated.
No spaces in the string s. It contains only a plurality of integers (each integer in the range between 1 and 1000), there is a "+" sign between each two adjacent partition certificate.

Output Format

You need to string s each element contains reorder, and outputs the result of sorting.
You need to make sure one element before and after the meet between each two adjacent elements result in not more than one element, and the two have a "+" sign separation between adjacent elements.

Sample input

3+2+1

Sample Output

1+2+3

Sample input

1+1+3+1+3

Sample Output

1+1+1+3+3

Sample input

2

Sample Output

2

Topic analysis

First, how to handle input?
We can see that the input is a digital + a character.
All the elements for a non-last element is, with the character behind it is "+";
for the last element, the characters are following it with "\ n" (there may be EOF).
So, we are more suitable for use with the C language scanf function.
Define a variable of type int num, and a char type variable c, and store all the elements of the array a [],
each scanf ( "% d% c" , & num, & c), the num into the array a [] and determine whether c is not "\ n" (or the EOF), to the input end.
Then the array a [] are sorted and output.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;

int num, a[110], n;
char c;

int main() {
    while (scanf("%d%c", &num, &c) != EOF) {
        a[n++] = num;
        if (c == '\n' || c == EOF) break;
    }
    sort(a, a+n);
    for (int i = 0; i < n; i ++) {
        if (i) putchar('+');
        printf("%d", a[i]);
    }
    putchar('\n');
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11450576.html