九度题目1434
来源:互联网 发布:海阔淘宝客助手 编辑:程序博客网 时间:2024/05/29 11:27
#include <stdio.h>
#include <algorithm>
using namespace std;
struct TV{
int s,e,l;
bool operator <(const TV &A)const{
return e<A.e;
}
}buf[100];
int main(){
int n,i;
while(scanf("%d",&n)!=EOF&&n!=0){
for(i=0;i<n;i++){
scanf("%d%d",&buf[i].s,&buf[i].e);
buf[i].l=buf[i].e-buf[i].s;
}
sort(buf,buf+n);
int ans=1;
int endtime=buf[0].e;
for(i=1;i<n;i++){
if(buf[i].s>=endtime){
ans++;
endtime=buf[i].e;
}
}
printf("%d\n",ans);
}
return 0;
#include <algorithm>
using namespace std;
struct TV{
int s,e,l;
bool operator <(const TV &A)const{
return e<A.e;
}
}buf[100];
int main(){
int n,i;
while(scanf("%d",&n)!=EOF&&n!=0){
for(i=0;i<n;i++){
scanf("%d%d",&buf[i].s,&buf[i].e);
buf[i].l=buf[i].e-buf[i].s;
}
sort(buf,buf+n);
int ans=1;
int endtime=buf[0].e;
for(i=1;i<n;i++){
if(buf[i].s>=endtime){
ans++;
endtime=buf[i].e;
}
}
printf("%d\n",ans);
}
return 0;
}
这是贪心算法的练习题
感觉还是很不熟练
首先注意结构体中定义了小于符号 以及定义的格式 bool operaor <(const TV &A)const { return e<A.e; }
还有贪心思路,此处是看哪个节目结束的早 然后累积
0 0
- 九度题目1434
- 九度题目1202
- 九度题目1061
- 九度题目1185
- 九度题目1023
- 九度题目1054
- 九度题目1096
- 九度题目1043
- 九度题目1070
- 九度题目1186
- 九度题目1018
- 九度题目1431
- 九度题目1156
- 九度题目1088
- 九度题目1065
- 九度题目1432
- 九度题目1052
- 九度题目1069
- C++语言
- some frequently-used formula
- gitHub上传本地代码
- [BZOJ3879] SvT
- 4.redis分片
- 九度题目1434
- Leetcode283 Move Zeros
- 关于notepad++和gcc绑定
- jdbc连接oracle的方法
- Stanford 机器学习笔记 Week6 Advice for Applying Machine Learning
- 三层初体验
- 搭建博客hexo
- Win32 GetLasrError 实现
- 简单配置搞定 Nginx + Tomcat + HTTPS