Search Here

Monday, June 8, 2015

UVa - 12541 ( Birthdates Solution )

Try Yourself First. For help Scroll Down .


Tip : You need to learn STL as my code . You can solve it by different way. I didn't tried it .Go Through it. You can learn some new thing. Here the keyword ' struct ' used to create a structure of Birthdays of different people. With one STL ( <algorithm> ) You can solve the whole problem . But along with this you have to create a compare function ( cmp ) which will return true or false of an statement.  and then you can solve it . To learn more about STL Click Here


Code :

#include<bits/stdc++.h>

using namespace std;

struct data{    // a structure created
    string name;
    int date, month , year;
};

bool cmp(data a, data b) // a compare function used to compare 
                         //different statements.
{
    if(a.year==b.year)
    {
        if(a.month==b.month)
        {
           return a.date<b.date;
        }
        else
             return a.month<b.month;
    }
    else
        return a.year<b.year;
}


int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        data man[100];
        string a;
        int d, m, y;
        for(int i=0; i<n; i++)
        {
            cin>>a>>d>>m>>y;
            man[i].name = a;
            man[i].date = d;
            man[i].month = m;
            man[i].year = y;
        }
        sort(man, man+n, cmp);
        //printf("%s\n%s\n", man[0].name, man[n-1].name);
        cout<<man[n-1].name<<endl<<man[0].name<<endl;
    }
    return 0;
}

=> Questions? Leave a Comment .

No comments:

Post a Comment