代码区:
#include<stdio.h>
#include<stdlib.h>
struct GAME{
int start;
int end;
};
int cmp(const void *a,const void *b){
struct GAME *game1=(struct GAME*)a;
struct GAME *game2=(struct GAME*)b;
return game1->end-game2->end;//将结构体按照结束时间升序排列
}
int main(){
int n;
scanf("%d",&n);
struct GAME *game=(struct GAME*)malloc(n*sizeof(struct GAME));
for(int i=0;i<n;i++){
scanf("%d%d",&game[i].start,&game[i].end);
}
qsort(game,n,sizeof(struct GAME),cmp);
int ans=1;
int end_time=game[0].end;
for(int i=0;i<n;i++){//用前一个结束时间和下一个开始时间作比较
if(game[i].start>=end_time){
ans++;
end_time=game[i].end;//更新结束时间
}
}
printf("%d",ans);
return 0;
}
欢迎各位读者提出意见。
(菜菜洛谷奋斗小日记)
标签:end,struct,int,GAME,P1803,ans,洛谷 From: https://blog.csdn.net/2402_88149600/article/details/145205691