代码:
#include<iostream>
#include<algorithm>
#define MAXN 100
using namespace std;
struct A{
int start;
int end;
}a[MAXN];
bool comp(A a, A b)
{
return a.end <= b.end;
}
void input(int n)
{
int i;
for (i = 0; i < n; i++)
cin >> a[i].start >> a[i].end;
}
void select(int n)
{
int sum = -1;
int count = 0;
for (int i = 0; i < n; i++)
{
if (a[i].start >= sum)
{
count++;
sum = a[i].end
}
}
cout << count << endl;
}
int main()
{
int n;
while (cin >> n)
{
if (n == 0)break;
input(n);
sort(a, a + n, comp);
select(n);
}
return 0;
}
这样写在pta会有堆栈溢出的问题或其他问题。后来改了a[MAX]为a[1000]就没有问题了。以后出现类似的问题可以解决了。