看了下别人的代码,自己想破头也想不到贪心算法,初学贪心算法,感觉这道题还挺典型
#include <iostream>
#include <string>#include<math.h>
#include<algorithm>
using namespace std;
struct tv{
int l;
int r;
};
bool cmp(tv a, tv b)
{
return a.r < b.r;
}
int main()
{
int n;
tv t[100];
int count;
while (cin >> n)
{
if (n == 0)
return -1;
for (int i = 0; i < n; i++)
{
cin >> t[i].l >> t[i].r;
}
sort(t, t + n, cmp);
int x = t[0].r;
count = 1;
for (int i = 1; i < n; i++)
{
if (t[i].l >= x)
{
count++;
x = t[i].r;
}
}
cout << count<<endl;
}
return 0;
}