首页 > 试题广场 >

Sign In and Sign Out (25)

[编程题]Sign In and Sign Out (25)
  • 热度指数:2760 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.

输入描述:
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.


输出描述:
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.
示例1

输入

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

输出

SC3021234 CS301133
跟记流水账差不多的。。。
package go.jacob.day826;

import java.util.Scanner;

/**
 * 1006. Sign In and Sign Out (25)
 * 
 * @author Jacob
 *
 */
public class Demo2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.nextLine());
		//ids,ins,outs分别用来保存每个用户的id,进入时间,退出时间
		String[] strs = new String[N];
		String[] ids = new String[N];
		String[] ins = new String[N];
		String[] outs = new String[N];
		for (int i = 0; i < N; i++) {
			strs[i] = sc.nextLine();
			ids[i] = strs[i].split(" ")[0];
			ins[i] = strs[i].split(" ")[1];
			outs[i] = strs[i].split(" ")[2];
		}
		int firstIndex = 0, lastIndex = 0;
		String earliest = ins[0];
		String latest = outs[0];
		for (int i = 1; i < N; i++) {
			//确定最早进入时间
			for (int j = 0; j < 3; j++) {
				if (compareTime(ins[i].split(":")[j], earliest.split(":")[j])) {
					break;
				} else if (compareTime(earliest.split(":")[j], ins[i].split(":")[j])) {
					earliest = ins[i];
					firstIndex = i;
				} else{
					continue;
				}
			}
			//确定最后退出时间
			for (int j = 0; j < 3; j++) {
				if (compareTime(latest.split(":")[j], outs[i].split(":")[j])) {
					break;
				} else if (compareTime(outs[i].split(":")[j], latest.split(":")[j])){
					latest = outs[i];
					lastIndex = i;
				} else {
					continue;
				}
			}
		}
		System.out.println(ids[firstIndex]+" "+ids[lastIndex]);

	}

	/*
	 * 如果s1>s2,返回true
	 */
	public static boolean compareTime(String s1, String s2) {
		return Integer.parseInt(s1) > Integer.parseInt(s2) ? true : false;
	}
} 


发表于 2017-08-26 10:31:45 回复(0)
#include <bits/stdc++.h>
using namespace std;
typedef struct node{
    char id[16];
    int h,m,s;
    node(){};
    node(int hh,int mm, int ss):h(hh),m(mm),s(ss){};
    bool operator > (const node& b) const {//比较时间大小
        if(h != b.h) return h > b.h;
        if(m != b.m) return m > b.m;
        return s > b.s;
    }
};
int main(){
    int n;
    node minx(23,59,59),maxx(0,0,0),temp;
    scanf("%d",&n);
    while(n--){
        scanf("%s %d:%d:%d",temp.id,&temp.h,&temp.m,&temp.s);
        if(minx>temp) minx = temp;
        scanf("%d:%d:%d",&temp.h,&temp.m,&temp.s);
        if(!(maxx>temp)) maxx = temp;
    }
    printf("%s %s\n",minx.id,maxx.id);
    return 0;
}

发表于 2019-04-02 15:30:55 回复(0)
//1018.Sign In and Sign Out
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct student
{
    string id,start,final;
};
vector<student> s;
bool cmp1(student a,student b){
    return a.start<b.start;
} //时间不用转化为int型,用字符串的字典序比较即可
bool cmp2(student a,student b){
    return a.final>b.final;
}
int main()
{
    int m; cin>>m;
    s.resize(m);
    for(int i=0;i<m;i++) cin>>s[i].id>>s[i].start>>s[i].final;
    sort(s.begin(),s.end(),cmp1);
    cout<<s[0].id<<" ";
    sort(s.begin(),s.end(),cmp2);
    cout<<s[0].id;
    return 0;
}
发表于 2019-03-03 17:28:03 回复(1)
本题关键是将数字串的时间统一用秒表示,然后再用系统的sort比较大小,但是遇到一个编译不通过的问题,我再本机都可以使用strcpy,但是oj上就提示编译错误,加上#include<cstring>就好了!

#include<stdio.h>

#include<cstring>

#include<string>

#include<iostream>

#include<vector>

#include<algorithm>

int getSTime(char time[9]);

using namespace std;


struct Stu

{

    char id[50];

    int startTime;

    int endTime;

};


int cmpStartTime(Stu a,Stu b)

{

    return a.startTime<b.startTime;

}


int cmpEndTime(Stu a,Stu b)

{

    return a.endTime>b.endTime;

}

int main()

{

    int N = 0;

    vector<Stu> results;

    scanf("%d",&N);

    for(int i=0;i<N;i++)

    {

        char tmpID[50];

        char tmpBegTime[9];

        char tmpEndTime[9];

        scanf("%s %s %s",tmpID,tmpBegTime,tmpEndTime);

        Stu tmp;

        strcpy(tmp.id,tmpID);

        tmp.startTime = getSTime(tmpBegTime);

        tmp.endTime = getSTime(tmpEndTime);

        results.push_back(tmp);        

    }


    sort(results.begin(),results.end(),cmpStartTime);

    cout<<results[0].id<<" ";

    sort(results.begin(),results.end(),cmpEndTime);

    cout<<results[0].id<<endl;

    return 0;

}


int getSTime(char time[9])

{

    int hour = (time[0]-48)*10+(time[1]-48);

    int minute = (time[3]-48)*10+time[4]-48;

    int s = (time[6]-48)*10+time[7]-48;

    return hour*3600+minute*60+s;

}

发表于 2018-02-15 08:23:54 回复(0)
import java.util.Scanner;
//直接字符串compareto方法比较(亲测时间更短),无需用spilit转换成int后比较
public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		String numear=in.next();
		String numlate=numear;
		String early=(in.next());
		String late=(in.next());
		for(int i=1;i<n;i++){
			String temp=in.next();
			String tempe=(in.next());
			String templ=(in.next());
			if(tempe.compareTo(early)<0){
				early=tempe;
				numear=temp;
			}
			if(templ.compareTo(late)>0){
				late=templ;
				numlate=temp;
			}
		}
		System.out.println(numear+" "+numlate);
	}

}


发表于 2016-10-28 15:50:32 回复(0)
#include<iostream>
using namespace std;

int main() {
	string id,in,out,min="23:59:59",max="00:00:00",minid,maxid;
	int M;
	cin>>M;
	while(M--){
		cin>>id>>in>>out;
		if(in<=min){
			min=in;
			minid=id;
		}
		if(out>=max){
			max=out;
			maxid=id;
		}
	}
	cout<<minid<<" "<<maxid;
    return 0;
}

发表于 2020-01-30 16:34:28 回复(0)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

class pnode {
public:
	string id,start,end;
};
bool cmp1(pnode* p1, pnode* p2) {
	return p1->start < p2->start;
}
bool cmp2(pnode* p1, pnode* p2) {
	return p1->end>p2->end;
}
//struct cmp {
//	bool operate()(pnode * p1, pnode* p2)const{
//
//	}
//};
void signinandsignout() {
	int n;
	string id, start, end;
	vector<pnode*> q;
	cin >> n;
	while (n--) {
		cin >> id >> start >> end;
		pnode* p = new pnode();
		p->id = id; p->start = start; p->end = end;
		q.push_back(p);
		//cout << (p->start > p->end);
	}
	sort(q.begin(), q.end()-1, cmp1);
	cout << (*q.begin())->id<<" ";
	sort(q.begin(), q.end() - 1, cmp2);
	cout << (*q.begin())->id ;
}
/*
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

*/int main()
{
	signinandsignout();
	return 0;
}

在pat上我错了一半,但是牛客网全对,谁能给看看
发表于 2020-01-20 23:26:51 回复(0)
#include <iostream>
#include <string.h>
using namespace std;
struct node 
{
    char ID[18];
    char sign_in[10];
    char sign_out[10];
};
int main()
{
    int n = 0;
    cin >> n;
    char min_sign_in[10], max_sign_out[10];
    char ans_id1[20], ans_id2[20];
    node temp;
    int i = 0, j = 0;    //把第一个人的相关信息先输进来
    cin >> temp.ID;
    cin >> temp.sign_in;
    cin >> temp.sign_out;
    strcpy(min_sign_in, temp.sign_in);
    strcpy(max_sign_out, temp.sign_out);
    strcpy(ans_id1, temp.ID);
    strcpy(ans_id2, temp.ID);
    for (i = 1; i < n; i++)  //在线记录和比较,不断的更新,时间上用字符串的比较即可
    {
        cin >> temp.ID;
        cin >> temp.sign_in;
        cin >> temp.sign_out;
        if (strcmp(min_sign_in, temp.sign_in) > 0)
        {
            strcpy(min_sign_in, temp.sign_in);
            strcpy(ans_id1, temp.ID);
        }
        if (strcmp(max_sign_out, temp.sign_out) < 0)
        {
            strcpy(max_sign_out, temp.sign_out);
            strcpy(ans_id2, temp.ID);
        }
    }    
    cout << ans_id1 << " " << ans_id2;
    return 0;
}

发表于 2018-08-15 15:04:31 回复(0)
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

struct Student
{
	string id;
	string arrive, leave;	
	void read(){
		cin >> id >> arrive >> leave;
	}
};

vector<Student> v;

bool compare1(Student s1, Student s2){
	return s1.arrive < s2.arrive;
}

bool compare2(Student s1, Student s2){
	return s1.leave > s2.leave;
}

int main(){
	int n;
	cin >> n;
	while(n--){
		Student s;
		s.read();
		v.push_back(s);
	}
	sort(v.begin(), v.end(), compare1);
	cout << v[0].id << " ";
	sort(v.begin(), v.end(), compare2);
	cout << v[0].id << endl;
	return 0;
}

编辑于 2017-08-01 21:25:48 回复(1)
注意一下数据的输入格式  开始时间和结束时间的比较直接比较string即可。
#include <iostream>
#include <string>

using namespace std;

int main(void)
{
	string ed = "00:00:00", st = "23:59:59", stno = "", edno = "";
	string s, t, no;
	int m;

	cin >> m;
	while (m--)
	{
		cin >> no >> s >> t;
		(s < st) ? (st = s, stno = no) : (st = st);
		(t > ed) ? (ed = t, edno = no) : (ed = ed);
	}

	cout << stno << " " << edno << endl;

	return 0;
}

发表于 2015-12-30 14:46:35 回复(0)
本文来自我的博客http://blog.csdn.net/sinat_29278271 ,还有其它许多题目的解题题解,欢迎访问纠错
   应该都会做的吧,只要记录每个人的时间,然后比一下谁最迟,谁最早就好了,不过偷懒的话可以使用stl中,时间的比较其实并不需要转化成int型,直接进行字符串的字典序比较就好了,了解到这一点的我突然觉得从前每道时间题都坚持写两个格式转换函数有些蠢。
# include <iostream>
# include <algorithm>
# include <string>
using namespace std;

typedef pair<string,string> tlog;
int main()
{
	int n;
	cin >> n;
	string a,b,c;
	cin >> a >> b >> c;
	tlog ear = tlog(b,a),las = tlog(c,a); 
	for (int i=1;i<n;i++)
	  {
	  	cin >> a >> b >> c;
	    ear = min(ear,tlog(b,a));
	    las = max(las,tlog(c,a));
	  }
    cout << ear.second << ' ' << las.second << endl;
    return 0;
}




发表于 2015-08-28 09:05:58 回复(1)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10;
const int inf = 0x3f3f3f3f;

map<string, string> st, ed;
int n;

int main() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        string a, b, c;
        cin >> a >> b >> c;
        st[b] = ed[c] = a;
    }
    cout << st.begin() -> second << ' ' << ed.rbegin() -> second << endl;
    return 0;
}
按题目描述不存在离开时间比开门时间要早,不存在进入时间比关门时间要晚,所以插入同一个map里即可,但是WA,所以可能这个思路是假的或者测试数据中出现了离开时间小于进入时间。
发表于 2022-11-20 11:23:06 回复(0)
大神的方法就是妙😍
#include<bits/stdc++.h>
using namespace std;

typedef pair<string,string> P;

int main(){
	int n;
	cin>>n;
	string a,b,c;
	cin>>a>>b>>c;
	P el=P(b,a),la=P(c,a);
	for(int i=1;i<n;i++){
		cin>>a>>b>>c;
		el=min(el,P(b,a));
		la=max(la,P(c,a));
	}
	cout<<el.second<<" "<<la.second<<endl;
	return 0;
}

发表于 2022-11-04 19:07:06 回复(0)
That is, the sign in time must be earlier than the sign out time for each person,
感觉牛客上的测试用例,有一些没有满足这个条件,也就是说有一些记录应该是无效的
发表于 2021-03-23 17:08:18 回复(0)
#include  <stdio.h>
#include  <malloc.h>
#include  <string.h>
struct menber
{
     char id[16];
    int hs,ms,ss,he,me,se;
};
int main()
{
    int n;
    int a,b;
    scanf("%d",&n);
   struct menber* M=(struct menber*)malloc((n+1)*sizeof(struct menber));
    int z=86400,w=0;
    for(int i=0;i<n;i++)
    {
        scanf("%s %d:%d:%d %d:%d:%d\n",M[i].id,&M[i].hs,&M[i].ms,&M[i].ss,&M[i].he,&M[i].me,&M[i].se);
              int x=M[i].hs*3600+M[i].ms*60+M[i].ss;
              int y=M[i].he*3600+M[i].me*60+M[i].se;
              if(x<z)   {z=x;a=i;}
              if(y>w)   {w=y;b=i;}
  
    }
        printf("%s %s\n",M[a].id,M[b].id);
return 0;
        
}
发表于 2020-05-26 18:23:22 回复(0)
m,n = '23:59:59','00:00:00'
for i in range(int(input())):
    b = input().split()
    if m > b[1]:
        m,p = b[1],b[0]
    if n < b[2]:
        n,q = b[2],b[0]
print(p,q)
编辑于 2020-03-13 11:31:45 回复(0)
package PAT;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Sign_In_Sign_Out {
	
	static class Node{
		String id,s,e;
		int start,end;
		public Node(String id,String s,String e) {
			this.id=id;this.s=s;this.e=e;
			String[] s1=s.split(":");
			String[] s2=e.split(":");
			start=Integer.parseInt(s1[0])*3600+Integer.parseInt(s1[1])*60+Integer.parseInt(s1[2]);
			end=Integer.parseInt(s2[0])*3600+Integer.parseInt(s2[1])*60+Integer.parseInt(s2[2]);
		}
	}

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		ArrayList<Node> list=new ArrayList<Node>();
		for(int i=0;i<n;i++) {
			Node te=new Node(sc.next(),sc.next(),sc.next());
		//	if(te.start<te.end) {
				list.add(te);
		//	}	
		//	System.out.println(te.id+" "+te.start+" "+te.end);

		}
		Comparator<Node> cmp1=new Comparator<Node>() {
			public int compare(Node a,Node b) {
				return a.start-b.start;
			}
		};
		Comparator<Node> cmp2=new Comparator<Node>() {
			public int compare(Node a,Node b) {
				return b.end-a.end;
			}
		};
		Collections.sort(list,cmp1);
		String a=list.get(0).id;
		Collections.sort(list,cmp2);
		String b=list.get(0).id;
		System.out.println(a+" "+b);
	}
}

发表于 2019-08-13 15:25:59 回复(0)
#include <iostream>
#include <string>
using namespace std;
 
int main() {     int M;  //当日进入教室的总人数     string ID, in, out;  //学生ID,进/出教室时间      string minID, maxID;  //最早和最晚进入教室的学生ID      string maxOut("00:00:00"), minIn("24:59:59");  //最早/最晚时间      cin>>M;      for (int i=1; i<=M; i++) {         cin>>ID>>in>>out;  //输入一人的数据,cin遇空格/换行结束          //求最早进入教室的时间          if (in<minIn) {             minIn = in;             minID = ID;         }         //求最晚离开教室的时间         if (out>maxOut) {             maxOut = out;             maxID = ID;         }     }              cout<<minID<<' '<<maxID;  //输出           return 0;
} 

发表于 2019-02-02 20:08:17 回复(0)
应该没有比我写得更短的了吧
import datetime
M,records=int(raw_input()),[]
for _ in range(M):
    record=raw_input().split()
    records.append([record[0],datetime.datetime.strptime(record[1],'%H:%M:%S'),datetime.datetime.strptime(record[2],'%H:%M:%S')])
print min(records,key=lambda c:c[1])[0],max(records,key=lambda c:c[2])[0]

发表于 2019-01-16 18:28:12 回复(0)
#include<iostream>
#include<string>

using namespace std;

//1006 Sign In and Sign Out


int main() {
    int m;//总记录数
    string id,sin,sout;
    
    string minn="24:00:00",maxx="00:00:00";
    string s_min,s_max;
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>id>>sin>>sout;
        if(sin<minn){ minn=sin; s_min=id;}
        if(sout>maxx) {maxx=sout; s_max=id;}
    }

    cout<<s_min<<" "<<s_max;
    
    return 0;
}
发表于 2019-01-13 12:00:09 回复(0)