九度:1013<开门人>

浙大2005年研究生机试


此题可排序解。这是看到别人的代码,自己又按排序做了一下,排序使用sort,两次排序,分别求最早与最晚。

自己的解法有两种:

第一时间作为字符串处理

作为字符串处理,时间也是可以直接比较的。

#include <stdio.h>
#include <iostream>
#include <cstring>

using namespace std;

struct People{
	char name[20];
	char ct[20];//come time
	char lt[20];//leave time
};


int main(){

#ifdef ONLINE_JUDGE
#else
    freopen("E:\\in.txt", "r", stdin);
#endif

	int days;
	while(scanf("%d", &days) != EOF){
		while(days-->0){
			People fcome, lleave;
			People t;
			int np;//num of people
			int i;

			scanf("%d", &np);
			for(i=0; i<np; i++){
				scanf("%s %s %s", t.name, t.ct, t.lt);

				if(i==0){
					strcpy(fcome.name, t.name);
					strcpy(fcome.ct, t.ct);

					strcpy(lleave.name, t.name);
					strcpy(lleave.lt, t.lt);
					continue;
				}

				if(strcmp(t.ct, fcome.ct) < 0){
					strcpy(fcome.name, t.name);
					strcpy(fcome.ct, t.ct);
				}// more ahead

				if(strcmp(t.lt, lleave.lt) > 0){
					strcpy(lleave.name, t.name);
					strcpy(lleave.lt, t.lt);
				}// more late
	
			}// for: compare

			cout << fcome.name << ' ' << lleave.name << endl;
		}
	}//while

	return 0;
}


第二设立专门的时间格式

#include <stdio.h>
#include <iostream>
#include <cstring>
 
using namespace std;
 
struct Time{
    int hour;
    int minute;
    int sec;
};
 
struct People{
    char name[20];
    struct Time ct;//come time
    struct Time lt;//leave time
 
 
};
 
int cmp(Time x, Time y){
    if(x.hour < y.hour)
        return -1;
    else if(x.hour > y.hour)
        return 1;
    else if(x.minute < y.minute)
        return -1;
    else if(x.minute > y.minute)
        return 1;
    else if(x.sec < y.sec)
        return -1;
    else
        return 1;
 
}
 
int main(){
 
#ifdef ONLINE_JUDGE
#else
    freopen("E:\\in.txt", "r", stdin);
#endif
 
    int days;
    while(scanf("%d", &days) != EOF){
        while(days-->0){
            People fcome, lleave;
            People t;
            int np;
            scanf("%d", &np);
            int i;
            for(i=0; i<np; i++){
                scanf("%s %d:%d:%d %d:%d:%d", t.name, &t.ct.hour, &t.ct.minute, &t.ct.sec,
                    &t.lt.hour, &t.lt.minute, &t.lt.sec);
 
                if(i==0){
                    strcpy(fcome.name, t.name);
                    fcome.ct.hour=t.ct.hour;
                    fcome.ct.minute=t.ct.minute;
                    fcome.ct.sec=t.ct.sec;
 
                    strcpy(lleave.name, t.name);
                    lleave.lt.hour=t.lt.hour;
                    lleave.lt.minute=t.lt.minute;
                    lleave.lt.sec=t.lt.sec;
                    continue;
                }
 
                if(cmp(t.ct, fcome.ct) < 0){
                    strcpy(fcome.name, t.name);
                    fcome.ct.hour=t.ct.hour;
                    fcome.ct.minute=t.ct.minute;
                    fcome.ct.sec=t.ct.sec;
                }// more ahead
 
                if(cmp(t.lt, lleave.lt) > 0){
                    strcpy(lleave.name, t.name);
                    lleave.lt.hour=t.lt.hour;
                    lleave.lt.minute=t.lt.minute;
                    lleave.lt.sec=t.lt.sec;
                }// more late
         
            }// for: compare
 
            cout << fcome.name << ' ' << lleave.name << endl;
        }
    }//while
 
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值