题目描述

比较简单的水题,只需要比较到达时间和离开时间即可,建立一个结构体存储最早时间和最晚时间,方便比较。
#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());
}
该博客介绍了一道比较简单的题目,只需比较到达和离开时间。解题时建立结构体存储最早和最晚时间,还可存储id和时间并重载<运算符。不必排序,在读入过程中比较得出到达最早和离开最晚的人的id,还涉及秒和字符串的比较。
968

被折叠的 条评论
为什么被折叠?



