Clearance Of Problem Statement :
To solve this problem you need to know about " STL " . I solved it with " STL & STRUCTURE ". All you have to do is to SORT the numbers under some CONDITIONS . Conditions are given below :
1) Sort the list into Ascending Order according to the Modulus of numbers. ( That means You have to find all Number's Modulus and then SORT the numbers INCREASINGLY according to the values of Modulus's. )
2) If you found the Modulus of Two Number is SAME then find EVEN & ODD between those.
3) If one of them is EVEN & the other is ODD then give the Priority to the ODD Number to appear FIRST in the sorted list .
4) If both of the Number is ODD then give the priority to the BIGGER ONE to appear FIRST in the sorted list .
5) If both of the Number is EVEN then give the priority to the SMALLER ONE to appear FIRST in the sorted list.
Tip : Don't forget to print the ' M N ' including ' 0 0 '.
Hope You Got It . Now You can Solve I think. If you can't then go to the Code.
Code :
#include<bits/stdc++.h>
using namespace std;
struct arr{
  int number;
  int mod;
};
bool odd(int x)
{
    if(x<0)  x = x* (-1);
    if(x%2!=0) return true;
    return false;
}
bool even(int x)
{
    if(x<0)  x = x* (-1);
    if(x%2==0) return true;
    return false;
}
bool cmp(arr x, arr y)
{
    if(x.mod<y.mod)
        return true;
    if(x.mod == y.mod)
    {
        if(odd(x.number) && even(y.number)) return true;
        if(odd(x.number) && odd(y.number) && (x.number>y.number)) return true;
        if(even(x.number) && even(y.number) && (x.number<y.number)) return true;
    }
    return false;
}
int main()
{
    int n, m;
    while(scanf("%d %d",&n, &m)==2)
    {
        printf("%d %d\n", n, m);
        if(m==0 && n==0) break;
        int x;
        arr a[10010];
        for(int i=0; i<n; i++)
        {
            cin>>x;
            a[i].number = x;
            a[i].mod = x%m;
        }
        sort(a, a+n, cmp);
        for(int i=0; i<n; i++)
            cout<<a[i].number<<endl;
    }
    return 0;
}
=> Need Help . Leave a Comment .

 
No comments:
Post a Comment