URI Online Judge Solution 1259 Even and Odd Using C++, Python Programming Language.
Considering the input of non-negative integer values, sort these numbers according to the following criteria: First the even in ascending order followed by the odd in descending order.
InputThe first line of input contains a positive integer number N (1 < N < 105). This is the number of following input lines. The next N lines contain, each one, a integer non-negative number.
OutputPrint all numbers according to the explanation presented above. Each number must be printed in one line as shown below.
Sample Input | Sample Output |
10 | 4 |
Solution Using C++:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int i,n;
cin>>n;
int arr[n];
for(i=0; i<n; i++)
{
cin>>arr[i];
}
sort(arr,arr+n);
for(i=0; i<n; i++)
{
if(arr[i]%2 == 0)
cout<<arr[i]<<endl;
}
for(i=n-1; i>=0; i--)
{
if(arr[i]%2 != 0)
cout<<arr[i]<<endl;
}
return 0;
}
Comments
Post a Comment