PAT 1006 Sign In and Sign Out

该博客介绍了一道比较简单的题目,只需比较到达和离开时间。解题时建立结构体存储最早和最晚时间,还可存储id和时间并重载<运算符。不必排序,在读入过程中比较得出到达最早和离开最晚的人的id,还涉及秒和字符串的比较。

题目描述

在这里插入图片描述
比较简单的水题,只需要比较到达时间和离开时间即可,建立一个结构体存储最早时间和最晚时间,方便比较。

#include<iostream>
using namespace std;
struct da{
	int hh;
	int mm;
	int ss;
};
int main(){
	int n;
	string fname,lname;
	da earlier;
	earlier.hh=24;
	da last;
	last.hh=0;
	string name;
	int hour,minute,second;
	char c;
	cin>>n;
	while(n--){
		cin>>name;
		cin>>hour>>c>>minute>>c>>second;
		if(hour<earlier.hh){
		fname=name;
		earlier.hh=hour;
		earlier.mm=minute;
		earlier.ss=second;}
		else if(hour==earlier.hh){
			if(minute<earlier.mm){
			
			fname=name;
		earlier.hh=hour;
		earlier.mm=minute;
		earlier.ss=second;
		}
			else if(minute==earlier.mm)
			{
				if(second<earlier.ss){
				
				fname=name;
				earlier.hh=hour;
		earlier.mm=minute;
		earlier.ss=second;
	}
			}
		}
	
		cin>>hour>>c>>minute>>c>>second;
		if(hour>last.hh){
				lname=name;
			last.hh=hour;
		last.mm=minute;
		last.ss=second;
		}
	
		else if(hour==last.hh){
			if(minute<last.mm)
			{
				lname=name;
			last.hh=hour;
		last.mm=minute;
		last.ss=second;
		}
			else if(minute==last.mm)
			{
				if(second<last.ss)
				{
				lname=name;
			last.hh=hour;
		last.mm=minute;
		last.ss=second;
		}
			}
		}
	}
	cout<<fname<<" "<<lname<<endl;
}

看了大佬写的代码,自己的代码真是又臭又长,自己都不想再看(
建立一个结构体,存储id和时间,重载了<运算符,由于只求到达时间最早和离开时间最晚的人的id,所以不必进行进行排序,直接在读入过程中进行比较得出结果即可。

#include<bits/stdc++.h>
using namespace std;
struct Person{
    string id="";
    int h,m,s;
    Person(int hh=0,int mm=0,int ss=0):h(hh),m(mm),s(ss){}
    bool operator <(const Person&p)const{//重载小于运算符
        if(this->h!=p.h)
            return this->h<p.h;
        else if(this->m!=p.m)
            return this->m<p.m;
        else
            return this->s<p.s;
    }
};
int main(){
    int N;
    scanf("%d",&N);
    Person p,minTime(INT_MAX,INT_MAX,INT_MAX),maxTime;
    for(int i=0;i<N;++i){
        cin>>p.id;
        scanf("%d:%d:%d",&p.h,&p.m,&p.s);
        minTime=min(minTime,p);
        scanf("%d:%d:%d",&p.h,&p.m,&p.s);
        maxTime=max(maxTime,p);
    }
    printf("%s %s",minTime.id.c_str(),maxTime.id.c_str());
    return 0;
}

比较秒

#include<iostream>
#include<string>
using namespace std;
int main(){
    string s1,s2;
    int n,max=0,min=86400;
    cin>>n;
    for(int i=0;i<n;i++){
        string stu;
        int h1,h2,m1,m2,se1,se2,t1,t2;
        cin>>stu;
        scanf("%d:%d:%d %d:%d:%d",&h1,&m1,&se1,&h2,&m2,&se2);
        t1=3600*h1+60*m1+se1;
        t2=3600*h2+60*m2+se2;
        if(t1<min){
            s1=stu;min=t1;
        }
        if(t2>max){
            s2=stu;max=t2;
        }
    }
    cout<<s1<<' '<<s2;
}

比较字符串

#include <iostream>
#include <string>
using namespace std;
string ansEarly, ansLate, early = "24:00:00", late = "00:00:00";
int main(){
	 int n;
	 scanf("%d",&n);
	 for(int i = 0; i < n; ++ i){
	 	string temp, tempIn, tempOut;
	 	cin>>temp>>tempIn>>tempOut;
	 	if(tempIn < early){
	 		ansEarly = temp;
	 		early = tempIn;
		}	
	 	if(tempOut > late){
	 		ansLate = temp;
	 		late = tempOut;
		}
	 }
	 printf("%s %s", ansEarly.c_str(), ansLate.c_str());
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值