HDU–1234:开门人和关门人

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。HDU–1234:开门人和关门人,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1. 题目源地址:http://acm.hdu.edu.cn/showproblem.php?pid=1234

2. 源代码:

给出自己写出的两个源代码吧,虽然不是很好的代码,但是毕竟是自己写出来的。

代码1:(其实可以优化成:把时间存成字符串,直接strcmp()比较字符串的大小来确定时间的早晚)

#include<iostream> 
#include<algorithm>
#include<string>
using namespace std;

struct node
{
   string ID;
   int start_H,start_M,start_S;
   int end_H,end_M,end_S;
}people[100000];

//签到时间排序函数 
int OpenDoor_cmp(const void *a,const void *b)
{
    struct node *c=(node *)a;
    struct node *d=(node *)b;
    
    if(c->start_H != d->start_H)
       return c->start_H - d->start_H;//优先按照小时排序 
       
    else if(c->start_M != d->start_M)
       return c->start_M - d->start_M;//其次按照分钟排序 
       
    else 
       return c->start_S - d->start_S;//最后按照秒钟排序 
}

//签离时间排序函数 
int CloseDoor_cmp(const void *a,const void *b)
{
    struct node *c=(node *)a;
    struct node *d=(node *)b;
    
    if(c->end_H != d->end_H)
       return d->end_H - c->end_H;//优先按照小时排序 
       
    else if(c->end_M != d->end_M)
       return d->end_M - c->end_M;//其次按照分钟排序 
       
    else 
       return d->end_S - c->end_S;//最后按照秒钟排序 
}

int main()
{
    int N,M,i;
    char ch;
    cin>>N;
    while(N--)
    {
       cin>>M;
       for(i=0;i<M;i++)
       {
          cin>>people[i].ID;
          cin>>people[i].start_H>>ch>>people[i].start_M>>ch>>people[i].start_S;
          cin>>people[i].end_H>>ch>>people[i].end_M>>ch>>people[i].end_S;               
       }
       
       qsort(people,M,sizeof(people[0]),OpenDoor_cmp);
       cout<<people[0].ID<<" ";
       
       qsort(people,M,sizeof(people[0]),CloseDoor_cmp);
       cout<<people[0].ID<<endl;
    }
    //system("pause");
    return 0;
}

代码2:将时间全部转化为整型秒,再来比较大小。

//HOJ--1234:开门人和关门人
#include<iostream>
#include<string>
using namespace std;

struct node
{
   string ID;
   int start;
   int end;    
}p[100000];

int OpenDoor_cmp(const void *a,const void *b)
{
    struct node *c=(node *)a;
    struct node *d=(node *)b;
    
    return c->start - d->start; 
}

int CloseDoor_cmp(const void *a,const void *b)
{
    struct node *c=(node *)a;
    struct node *d=(node *)b;
    
    return d->end - c->end; 
}

int main()
{
    int N,M;
    int i,j;
    char ch;
    int start_H,start_M,start_S;
    int end_H,end_M,end_S;
    
    cin>>N;
    while(N--)
    {
       cin>>M;
       for(i=0;i<M;i++)
       {
          cin>>p[i].ID;
          cin>>start_H>>ch>>start_M>>ch>>start_S;
          cin>>end_H>>ch>>end_M>>ch>>end_S;
          
          p[i].start=start_H*60*60+start_M*60+start_S;
          p[i].end=end_H*60*60+end_M*60+end_S;
       }
       
       qsort(p,M,sizeof(p[0]),OpenDoor_cmp);
       cout<<p[0].ID<<" ";
       
       qsort(p,M,sizeof(p[0]),CloseDoor_cmp);
       cout<<p[0].ID<<endl;
    }
    //system("pause");
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/163052.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!