按照结束时间从早到晚看,要是当前时间小于开始时间,则可以看。每次看完时间变为结束时间。
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; struct Program{ int start; int end; }; Program programs[101]; bool cmp(Program a, Program b){ return a.end < b.end; } int main(){ int n; int ans, now; while (cin >> n) { if(n == 0) break; ans = 0; now = 0; for(int i = 0; i < n; i++){ cin >> programs[i].start >> programs[i].end; } sort(programs, programs + n, cmp); // for(int i = 0; i < n; i++){ // cout << programs[i].end << endl; // } for(int i = 0; i < n; i++){ if(now <= programs[i].start){ //cout << programs[i].start << endl; now = programs[i].end; ans++; } } cout << ans << endl; } return 0; }