PAT (Advanced Level)1006解

因为最近在刷题库,想想就把本人可以想到的解法写到博客里,作为整理归纳。未必是最优解,还请各位高手多多包涵,能够指点指点。

题目要求
1006 Sign In and Sign Out (25 分)
At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification:

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time

where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters.

Output Specification:

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input:

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample Output:

SC3021234 CS301133

解题思路
主要由于太久没有写过链表了,看到这题就想到直接用了,其实这题用数组会明显简单的多。
注释已经写得很详细了,思路也挺简单了,就不赘述了。
注意事项
题目简单,没有什么特殊边界值。
代码部分

#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
struct node{
	string id;
	long int signintime;
	long int signouttime;
	struct node *next;
};
main()
{
	int m=1;
	scanf("%d",&m);//读入m
	node *r=new node;//r存放头 
	node *p=r,*q=r;//p是当前,q用来存放p的前一个 
	int h,min,s;
	for(int i=0;i<m;i++){
		cin>>p->id;
		scanf("%d:%d:%d ",&h,&min,&s);
		p->signintime=h*60*60+min*60+s;
		scanf("%d:%d:%d",&h,&min,&s);
		p->signouttime=h*60*60+min*60+s;		
		if(i!=0){
			//如果不是第一次,那么就要插链表 
			q->next=p;
			q=q->next; 

		}
		p=new node;//注册下一个结点备用 
	}
	//通过循环比较大小的办法找出第一个进入的id
	delete(p);//删除多余结点
	p=r; 
	string id;
	long int time;
	for(int i =0;i<m;i++){
		if(i==0){
			id=p->id;
			time=p->signintime;
		}
		if(p->signintime<time){
			id=p->id;
			time=p->signintime;
		}
		p=p->next;
	}
	cout<<id;
	printf(" ");//中间用空格间隔开 
	//再输出最晚离开的id
	p=r; 
	for(int i =0;i<m;i++){
		if(i==0){
			id=p->id;
			time=p->signouttime;
		}
		if(p->signouttime>time){
			id=p->id;
			time=p->signouttime;
		}
		p=p->next;
	}
	cout<<id;
	//释放空间
	p=r;
	while(p!=NULL){
		p=p->next;
		delete(r);
		r=p;
	} 
		 
 }    
			

运行结果
在这里插入图片描述
网上还有看到一个代码简单简洁的解法,用的就是数组。
网上代码部分

#include<stdio.h>  
    #include<string.h>  
    int main(){  
        char lkman[20],unlkman[20];  
        char id[20],in[20],out[20];  
        char fst[20]={"24:00:00"},last[20]={"00:00:00"};  
        int i,j,n;  
        scanf("%d",&n);  
        for(i=0;i<n;i++){  
            scanf("%s %s %s",id,in,out);  
            if(strcmp(in,fst)<0){  
                strcpy(fst,in);  
                strcpy(unlkman,id);  
            }  
            if(strcmp(out,last)>0){  
                strcpy(last,out);  
                strcpy(lkman,id);  
            }  
        }  
        printf("%s %s\n",unlkman,lkman);  
        return 0;  
    }

代码转载自:http://blog.csdn.net/sup_heaven/article/details/8451669

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值