HDOJ 2037

贪心入门题

#include <iostream>
#include <algorithm>

using namespace std;

struct Note
{
    int s;
    int e;
}T[102];

int cmp(const void *a,const void *b)
{

    if(((Note*)a)->e>((Note*)b)->e)
        return 1;
    else
        return -1;
}

int main()
{
    int n;
  while(cin>>n&&n)
  {
    for(int i=0;i<n;i++)
    {
        cin>>T.s>>T.e;
    }

    qsort(T,n,sizeof(Note),cmp);

    int ans=1;

    int i=1;
    int me=T[0].e;
    while(i<n)
    {
        if(T.s>=me)
        {
            me=T.e;
            ans++;
        }
        i++;
    }

    cout<<ans<<endl;
  }
/*
    for(int i=0;i<n;i++)
    {
        cout<<T.s<<" "<<T.e<<endl;
    }
*/
    return 0;
}

原文地址:https://www.cnblogs.com/CKboss/p/3351126.html