非洲小孩
来源:互联网 发布:smi s java 编辑:程序博客网 时间:2024/03/29 13:23
</pre>非洲小孩</h2><div class="problem-ins">时间限制:<span id="problem[time_limit]" class="editable highlight">1000</span> ms | 内存限制:<span id="problem[memory_limit]" class="editable highlight">65535</span> KB</div><div class="problem-ins">难度:<span class="editable highlight">2</span></div></div><div class="clr"></div><dl class="problem-display"><dt>描述 </dt><dd>家住非洲的小孩,都很黑。为什么呢?第一,他们地处热带,太阳辐射严重。第二,他们不经常洗澡。(常年缺水,怎么洗澡。)现在,在一个非洲部落里,他们只有一个地方洗澡,并且,洗澡时间很短,瞬间有木有!!(这也是没有的办法,缺水啊!!)每个小孩有一个时间段能够洗澡。并且,他们是可以一起洗的(不管你是男孩是女孩)。那么,什么时间洗澡,谁应该来洗,由谁决定的呢?那必然是他们伟大的“澡”神啊。“澡”神有一个时间表,记录着该部落的小孩,什么时候段可以洗澡。现在,“澡”神要问你,一天内,他需要最少开启和关闭多少次洗澡的水龙头呢?因为,开启和关闭一次水龙头是非常的费力气的,即便,这也是瞬间完成的。<div class="clr"></div><dl class="others"><dt>输入</dt><dd>多组数据第一行一个0<n<=100。接下来n行,每行一个时间段。H1H1:M1M1-H2H2:M2M2,24小时制。保证该时间段是在一天之内的。但是,不保证,H1H1:M1M1先于H2H2:M2M2。</dd><dt>输出</dt><dd>题目描述,“澡”神最少需要开启和关闭多少次水龙头呢?</dd><dt>样例输入</dt><dd><pre id="sample_input">100:12-12:12200:12-12:1214:00-12:00
样例输出 11
#include<iostream>#include<algorithm>#include<stdio.h>#include<cmath>using namespace std;typedef struct dot{ int begin,end;}dot;bool cmp(dot a,dot b){ return a.begin<b.begin;};int main(){ int x1[100],y1[100],x2[100],y2[100],n; dot a[100]; while(cin>>n) { for(int i=0;i!=n;i++){ scanf("%d:%d-%d:%d",&x1[i],&y1[i],&x2[i],&y2[i]); a[i].begin=x1[i]*100+y1[i]; a[i].end=x2[i]*100+y2[i]; if(a[i].begin>a[i].end)swap(a[i].begin,a[i].end); } sort(a,a+n,cmp); int cnt=1; for(int i=1;i!=n;i++) if(a[i].begin>a[i-1].end)cnt++; else a[i].end=min(a[i].end,a[i-1].end); cout<<cnt<<endl; } return 0;}
最优代码:copy:
#include<cstdio>#include<algorithm>struct time{ int x, y; bool operator < (const time& rhs) const{ return y < rhs.y; }}tim[110];using namespace std;int main(){ int n, h1, h2, m1, m2; while(~scanf("%d", &n)){ for(int i = 0; i < n; i++){ scanf("%d:%d-%d:%d", &h1, &m1, &h2, &m2); tim[i].x = min(h1*60+m1, h2*60+m2); tim[i].y = max(h1*60+m1, h2*60+m2); } sort(tim, tim+n); int t = 0; int ma = -1; for(int i = 0; i < n; i++){ if(tim[i].x>ma){ ma = tim[i].y; t++; } } printf("%d\n", t); } return 0;}
这道题的方法很好想,但是有一点卡屯就是时间的比较那一点,然后我就直接把它的时值直接*100,再加上后面的分值;这样虽然水过了但是还是不妥;看了人家的代码才知道这种重载运算符的方法可以直接比较大小:
<pre name="code" class="cpp">struct time{ int x, y; bool operator < (const time &rhs) const{ return y < rhs.y; }}tim[110];
应该还可以写成这样:
struct time{ int x, y; bool operator < (const time &rhs) const { if(x<rhs.x)return true; else return y < rhs.y; }}tim[110];
这个比较好懂,就是先比较x,较大的那个大,若相等则比较y,后面那个const说明是常成员函数。<pre id="best-content-1008263205" class="best-text mb-10" accuse="aContent">常成员函数可以理解为是一个“只读”函数,它既不能更改数据成员的值,也不能调用那些能引起数据成员值变化的成员函数,只能调用const成员函数。
11
#include<iostream>#include<algorithm>#include<stdio.h>#include<cmath>using namespace std;typedef struct dot{ int begin,end;}dot;bool cmp(dot a,dot b){ return a.begin<b.begin;};int main(){ int x1[100],y1[100],x2[100],y2[100],n; dot a[100]; while(cin>>n) { for(int i=0;i!=n;i++){ scanf("%d:%d-%d:%d",&x1[i],&y1[i],&x2[i],&y2[i]); a[i].begin=x1[i]*100+y1[i]; a[i].end=x2[i]*100+y2[i]; if(a[i].begin>a[i].end)swap(a[i].begin,a[i].end); } sort(a,a+n,cmp); int cnt=1; for(int i=1;i!=n;i++) if(a[i].begin>a[i-1].end)cnt++; else a[i].end=min(a[i].end,a[i-1].end); cout<<cnt<<endl; } return 0;}最优代码:copy:
#include<cstdio>#include<algorithm>struct time{ int x, y; bool operator < (const time& rhs) const{ return y < rhs.y; }}tim[110];using namespace std;int main(){ int n, h1, h2, m1, m2; while(~scanf("%d", &n)){ for(int i = 0; i < n; i++){ scanf("%d:%d-%d:%d", &h1, &m1, &h2, &m2); tim[i].x = min(h1*60+m1, h2*60+m2); tim[i].y = max(h1*60+m1, h2*60+m2); } sort(tim, tim+n); int t = 0; int ma = -1; for(int i = 0; i < n; i++){ if(tim[i].x>ma){ ma = tim[i].y; t++; } } printf("%d\n", t); } return 0;}
这道题的方法很好想,但是有一点卡屯就是时间的比较那一点,然后我就直接把它的时值直接*100,再加上后面的分值;这样虽然水过了但是还是不妥;看了人家的代码才知道这种重载运算符的方法可以直接比较大小:
<pre name="code" class="cpp">struct time{ int x, y; bool operator < (const time &rhs) const{ return y < rhs.y; }}tim[110];
应该还可以写成这样:
struct time{ int x, y; bool operator < (const time &rhs) const { if(x<rhs.x)return true; else return y < rhs.y; }}tim[110];
这个比较好懂,就是先比较x,较大的那个大,若相等则比较y,后面那个const说明是常成员函数。<pre id="best-content-1008263205" class="best-text mb-10" accuse="aContent">常成员函数可以理解为是一个“只读”函数,它既不能更改数据成员的值,也不能调用那些能引起数据成员值变化的成员函数,只能调用const成员函数。
0 0
- 非洲小孩
- 非洲小孩
- 非洲小孩
- 非洲小孩
- 非洲小孩
- 非洲小孩
- 非洲小孩
- 非洲小孩
- NYOJ1036 非洲小孩 【贪心】
- NYOJ-1036 非洲小孩
- NYOJ1036 非洲小孩
- 1036 非洲小孩
- 1036 非洲小孩
- 1411010737-ny-非洲小孩
- NYOJ 非洲小孩
- nyoj 1036非洲小孩
- NYOJ 1036 非洲小孩
- nyoj1036 非洲小孩
- 对java中常用的集合几点看法
- 单例模式
- 剑指offer系列-用两个栈实现队列
- 什么是OBS
- Python起个web服务
- 非洲小孩
- 工厂模式
- 大数据架构:flume-ng+Kafka+Storm+HDFS 实时系统组合
- nyoj题目13:Fibonacci数
- Spring原理探索第一步-以excel导入为列探讨SpringIoc原理
- 如何估算网站日承受最大访问PV
- 流操作的基本步骤
- 【Foundation Frame】NSDictionary/NSMutableDictionary
- 代理模式