A question about sort on the Openjudge.

Source: Internet
Author: User

Let's give you the title first.

Openjudge Simple sort 06 integer odd-even sort

Total time limit:
1000ms
Memory Limit:
65536kB
Describe

Given a sequence of 10 integers, it is required to reorder it. Sorting requirements:

1. Odd in front, even in the rear;

2. Odd-numbered sort from large to small;

3. Even order from small to large.

Input
Enter a line that contains 10 integers separated by a single space, with each integer having a range greater than or equal to 0, and less than or equal to 100.
Output
after sorting according to the requirements, the output line contains 10 integers after sorting, separated by a space between the number and the number.
Sample input
4 7 3 13 11 12 0 47 34 98
Sample output
47 13 11 7 3 0 4 12 34 98

I do not know what you feel after reading the topic. Anyway, as a little rookie who just learned to use sort, it looks like it's hard. (with the quick row, the bucket row, the bubble platoon big guy does not spray. )
The main point is that this is not a simple sort, first of all, it is required to order after the odd number in the first even after the odd number to be from the big to the small row, even the smallest to the big row, so here will be odd and even the number is stored in two
The count continues to be calculated in the array.
The first step is to define it first:
int ji[12],o[12],sum=1,ans=1,m;
To explain, here the books are first integers, ji[12] for storing odd numbers, ou[12] for storing even, sum and ans represent the number of odd and even occurrences in 10 digits (i.e., odd and even
Number) then M represents the number of m at the time of the loop input. (It doesn't matter if you don't understand it now, you'll see the code later)
First know to sort the number of only 10, this kind of topic is still very conscience, but these 10 numbers have odd, it must first must be read into the 10 numbers classified a wave, divided into odd and even
Two arrays to store separately, then the code is basically as follows:

for (int i=0;i<10;i++)
{
cin>>m;
if (m%2!=0)
{
Ji[sum]=m;
sum++;
}
Else
{
O[ans]=m;
ans++;
}
}
The above code may be inappropriate in some places, the great God do not spray.
So to illustrate the reading and classification here, first open a loop of 10 times for the loop, and then each time the loop read into M, which is a If-else SELECT statement to determine the parity of M, that is, if
m%2! =0 the remainder of M divided by two is not equal to 0, that is, M is an odd number, so the M is deposited ji[sum], at this time sum=1, but when this condition is executed once, the value of sum will be +1;
If else, that is, m%2==0, then the value of M is deposited into the array O[ans], similarly, the value of ans is +1 for each other else condition, so that the 10 numbers read into the class are good;

After classification, then it is time to start sorting, the JI array is stored in an odd number, the OU array stores an even number, first the classification code for the Big guy to serve:
Sort (ji+0,ji+sum);
Sort (O+0,o+ans);
Is it a little crazy to read it? Yes, the odd number of topics required is to order from large to small, and sort () The default sort is from small to large, but now we start from small to large sort, not anxious, a
We'll finish the output. Of course if you want to sort from the big to the small, it is also possible to format the sort (start value, end value, comp), but here you need to write a comp function,
Specific code:
int comp (const int &a,const int &b)
{
Return a>b;
}
After writing this function, you can use comp directly.
So here we first put the odd even number in order, the next can be directly output. The code Presents:

for (int i=sum-1;i>0;i--)
{
cout<<ji[i]<< "";
}
for (int i=1;i<ans;i++)
{
cout<<o[i]<< "";
}
See here some big guy must already understand, before odd reason with sort of small to big sort, is because actually did not need so troublesome to write comp function, directly on the output link before the sequence of Ji array inverted
The output of the sequence is not good? For example, such as a<b<c. So the reverse output is the c>b>a, let yourself experience it. Even that is not what to say, directly typesetting the output can be good sequence.

Finally, a word of caution ... Don't forget to return 0;
Now on the AC full-out code, and then repeat: Big God do not spray.

#include <bits/stdc++.h>
using namespace Std;
int ji[100],o[100],sum=1,ans=1,m;
int main ()
{
for (int i=0;i<10;i++)
{
cin>>m;
if (m%2!=0)
{
Ji[sum]=m;
sum++;
}
else if (m%2==0)
{
O[ans]=m;
ans++;
}
}
Sort (ji+0,ji+sum);
Sort (O+0,o+ans);
for (int i=sum-1;i>0;i--)
{
cout<<ji[i]<< "";
}
for (int i=1;i<ans;i++)
{
cout<<o[i]<< "";
}
return 0;
}

If this blog is helpful to you, please praise Qaq.















A question about sort on the Openjudge.

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.