Codeforces 339A Helpful Maths (greedy implementation sortings strings water problem)

Source: Internet
Author: User
Tags time limit
A. Helpful Maths
Click to open the topic
Time limit per test 2 seconds memory limit per test megabytes input standard input output standard output

Xenia The beginner mathematician is a third year student at elementary school. She's now learning the addition operation.

The teacher have written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum is only contains numbers 1, 2 and 3. Still, that's isn ' t enough for Xenia. She is only beginning to count, so she can calculate a sum only if the summands follow in non-decreasing order. For example, she can ' t calculate sum 1+3+2+1 But she can calculate sums 1+1+2 and 3+3.

You ' ve got the sum of that is written on the board. Rearrange the Summans and print the sum in such a to that Xenia can calculate the sum. Input

The first line contains a non-empty string s-the sum Xenia needs to count. String s contains no spaces. It is only contains digits and characters "+". Besides, string s is a correct sum of numbers 1, 2 and 3. String S is at the most characters long. Output

Print the new sum that Xenia can count. Sample Test (s) Input

3+2+1
Output
1+2+3
Input
1+1+3+1+3
Output
1+1+1+3+3
Input
2
Output
2 The
problem is the order from small to large output on the line, the ' + ' number unchanged.
Code:
#include <iostream> #include <string.h> #include <stdlib.h> using namespace std;
    int cmp (const void *a,const void *b) {return * (int *) a-* (int *) b;} int main () {char s[101];
    int a[101],i,j,k;
        while (cin>>s) {K=strlen (s);
            for (i=0,j=0;i<k;i++) {if (i%2==0) {a[j++]=s[i]-' 0 ';
        }} qsort (A,j,sizeof (a[0]), CMP);
            for (i=0,j=0;i<k;i++) {if (i%2==0) {s[i]=a[j++]+ ' 0 ';
        }} cout<<s<<endl;
    memset (S, ' n ', sizeof (s));
} return 0;

Another code that comes with another introduction on the Web: <pre name= "code" class= "CPP" > #include <iostream> using namespace std;
	int main () {string S;
	int I, J;
	char x;
	cin>>s;
	For (I=s.size ()-1; i>0; i-=2) for (j=0; j<i; j+=2) if (s[j]>s[j+2]) {x = S[j]; S[j] = s[j+2]; s[j+2] = x;
	} cout<<s;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.