Wednesday, March 22, 2017

Solution of Light OJ 1109 - False Ordering

/*  Bismillahir Rahmanir Rahim
    Saving number of divisor for every number and
    sorting using structure.
*/
#include<bits/stdc++.h>
using namespace std;
int t, n, i, j, cs=1;

struct all{
    int num;
    int ds;
}one[1001];

bool comp(all f, all s){ // sort of structure
    if(f.ds==s.ds) return f.num>s.num;
    else return f.ds<s.ds;
}

void cal(){  // main calculation for all numbers
    for(i=1; i<=1000; i++){
        one[i].num=i;
        one[i].ds=1;
    }
    for(i=1; i<=500; i++){
        for(j=2*i; j<=1000; j+=i){
            one[j].ds=one[j].ds+1;
        }
    }
}

int main(){
    cal();
    sort(one, one+1001, comp);
    cin>>t;
    while(t--){
        cin>>n;
        cout<<"Case "<<cs++<<": "<<one[n].num<<endl;
    }
    return 0;
}

No comments:

Post a Comment