输入n个节目,每个节目的开始时间和结束时间(假设都为整数),利用贪心算法求最多能收看最多的完整节目
#include<stdio.h>
#include<algorithm>using namespace std;
struct program{
int starTime;
int endTime;
bool operator<(const program &A) const{
return endTime<A.endTime;
}
}buf[100];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(n==0) break;
for(int i=0;i<n;i++){
scanf("%d%d",&buf[i].starTime,&buf[i].endTime);
sort(buf,buf+n);
int currentTime=0,ans=0;
for(int i=0;i<n;i++){
if(currentTime<=buf[i].starTime){
currentTime=buf[i].endTime;
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
}