详细题解
2025-09-27 16:48:54
发布于:湖北
2阅读
0回复
0点赞
#include <iostream>
#include <algorithm>
using namespace std;
struct Show {
int start, end;
};
bool cmp(Show a, Show b) {
return a.end < b.end; // 按结束时间升序排序
}
int main() {
int n;
Show shows[105]; // 最多 100 个节目,开大一点保险
while (cin >> n && n != 0) {
// 输入 n 个节目的开始和结束时间
for (int i = 0; i < n; i++) {
cin >> shows[i].start >> shows[i].end;
}
// 排序
sort(shows, shows + n, cmp);
int count = 0;
int lastEnd = 0; // 上一个看的节目的结束时间
for (int i = 0; i < n; i++) {
if (shows[i].start >= lastEnd) { // 能完整看
count++;
lastEnd = shows[i].end; // 更新结束时间
}
}
cout << count << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个