编辑代码

#include <bits/stdc++.h>

using namespace std;

struct Time {
    int begin;
    int end;
};

int ans = 0;
Time times[101];

bool cmp(Time a,Time b)
{
    return a.end < b.end;  //  越早结束越好
}
int main()
{
    int n;
    while (cin >> n && n != 0)
    {  
        for(int i = 0; i < n; i++) {
            cin >> times[i].begin >> times[i].end;
        } 
        sort(times, times + n, [&](Time a, Time b) {
            return a.end < b.end;
        });
        int lastProgram_end = times[0].end;
        ans = 1;  //  第一个节目必须加上
        for(int i = 1; i < n; i++) {
            if (lastProgram_end <= times[i].begin) { // 上一个节目结束了 才可以开始下一个节目
                ans++;
                lastProgram_end = times[i].end;
            }
        }
        cout << ans << endl;
    }
    return 0;
}