PAT 1006. Sign In and Sign Out (25)
来源:互联网 发布:日立柜机 知乎 编辑:程序博客网 时间:2024/05/20 00:12
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:3CS301111 15:30:28 17:00:10SC3021234 08:00:00 11:25:25CS301133 21:45:00 21:58:40Sample Output:
SC3021234 CS301133
提交代码
解法1:直接用sort函数整。
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct E{char no[17];char in[10];char out[10];}r[10000];bool cmp1( E a , E b ) {return strcmp( a.in,b.in ) < 0;}bool cmp2(E a,E b){return strcmp(a.out,b.out)>0;}int main(){int n,i;while(scanf( "%d" , &n )!=EOF){for( i = 0 ; i < n ; i++ ){scanf( "%s%s%s" , r[i].no , r[i].in , r[i].out );}sort( r , r+n , cmp1 ); //按开始时间由小到大排printf( "%s " , r[0].no );sort( r , r+n , cmp2 ); //按结束时间由大到小排printf( "%s\n" ,r[0].no );}return 0;}
解法2:依次比较。
//把时间直接转化为秒,最有利于计算。。。。。。//时间输入格式:必须要用%*c,否则0分。。。。。#include<stdio.h>#include<string.h>int minTime=1000000,maxTime=0,size=0;struct E{char id[20];int in;int out;}r[100],tmp1,tmp2; //结构体后有分号啊。。。tmp1保存最早来的人,tmp2保存最后走的人int main(){int n,h1,m1,s1;int i,h2,m2,s2;char a[20];//E tmp1,tmp2; //直接用名字,不带struct。。while(scanf("%d",&n)!=EOF){while(n--){scanf("%s%d%*c%d%*c%d%d%*c%d%*c%d",a,&h2,&m2,&s2,&h1,&m1,&s1);//必须要用%*c否则0分。。。。。strcpy(r[size].id,a); //把后面的赋给前面的。。字符串不能直接赋值!!r[size].in=3600*h2+60*m2+s2;r[size].out=3600*h1+60*m1+s1;size++;}for(i=0;i<size;i++){if(r[i].in<minTime){minTime=r[i].in;tmp1=r[i];}if(r[i].out>maxTime){maxTime=r[i].out;tmp2=r[i];}}printf("%s %s\n",tmp1.id,tmp2.id);}return 0;}
- PAT 1006. Sign In and Sign Out
- PAT 1006. Sign In and Sign Out
- 【PAT】1006. Sign In and Sign Out
- PAT-Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)-PAT
- 【PAT】1006. Sign In and Sign Out (25)
- 【PAT Advanced Level】1006. Sign In and Sign Out (25)
- PAT 1006. Sign In and Sign Out (25)
- PAT A 1006. Sign In and Sign Out (25)
- 【PAT (Advanced Level)】1006. Sign In and Sign Out (25)
- PAT 1006. Sign In and Sign Out (25)
- PAT 1006. Sign In and Sign Out (25)
- [PAT]1006. Sign In and Sign Out (25)
- PAT 1006. Sign In and Sign Out (25)
- PAT A 1006. Sign In and Sign Out (25)
- PAT 1006. Sign In and Sign Out (25)
- pat 1006. Sign In and Sign Out (25)
- [PAT]1006. Sign In and Sign Out (25)
- 图说微积分(八)极限
- Http协议的流程以及相关内容(1.0与1.1的区别)
- centos源码安装tomcat
- 正则表达式学习笔记
- Android AlarmManager设置多个定时事件时只最后一个有效问题解决
- PAT 1006. Sign In and Sign Out (25)
- Java编程提高性能时需注意的地方
- cocos2dx 常用工具网站荟萃
- linux shell 数组的使用
- sqlserver锁表、解锁、查看销表
- JAVA 的文件操作
- 算法操作符和关系操作符
- Linux内核开发修炼之路(不含认证),正在报名中
- php apache配置