非洲小孩

</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">1
00:12-12:12
2
00:12-12:12
14:00-12:00
样例输出
1
1
 
  
#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成员函数。
 
 

转载于:https://www.cnblogs.com/NYNU-ACM/p/4248814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值