sort排序问题
问题
1006SignInandSignOut(25分)
Atthebeginningofeveryday,thefirstpersonwhosignsinthecomputerroomwillunlockthedoor,andthelastonewho
herecordsofsigningin’sandout’s,youaresuppodtofindtheoneswhohave
unlockedandlockedthedooronthatday.
InputSpecification:
estartswithapositiveintegerM,
whichisthetotalnumberofrecords,followedbyMlines,eachintheformat:
ID_numberSign_in_timeSign_out_time
wheretimesaregivenintheformatHH:MM:SS,andID_numberisastringwithnomorethan15characters.
OutputSpecification:
Foreachtestca,outputinonelinetheIDnumbersofthepersonswhohaveunlockedandlockedthedooronthatday.
ThetwoIDnumbersmustbeparatedbyonespace.
Note:,thesignintimemustbeearlierthanthesignouttimefor
eachperson,andtherearenotwopersonssigninoroutatthesamemoment.
SampleInput:
3
CS30111115:30:2817:00:10
SC302123408:00:0011:25:25
CS30113321:45:0021:58:40
结尾⽆空⾏
SampleOutput:
SC3021234CS301133
结尾⽆空⾏
思路
利⽤sort函数对两个结构体数组按照不同的规则排序。对于in数组,按照⼩时,分钟,秒的权重从⼩到⼤排序,最⼩的元素即为最早签到的
⼈。对于out数组,按照⼩时,分钟,秒的权重从⼤到⼩排序,最⼩的元素即为最晚签离的⼈。
代码
#include
usingnamespacestd;
constintmaxn=1010;
structnode{
stringid;
intHH,MM,SS;
node(){}
node(string_id,int_HH,int_MM,int_SS):id(_id),HH(_HH),MM(_MM),SS(_SS){}
};
structnodein[maxn],out[maxn];
intm;
boolcmpIn(nodea,nodeb){
if(!=)
<;
el{
if(!=)
<;
el
<;
}
}
boolcmpOut(nodea,nodeb){
if(!=)
>;
el{
if(!=)
>;
el
>;
}
}
intmain(){
intH,M,S,H1,M1,S1;
cin>>m;
stringid;
for(inti=0;i
cin>>id;
getchar();
scanf("%d:%d:%d",&H,&M,&S);
getchar();
scanf("%d:%d:%d",&H1,&M1,&S1);
nodeinPerson=node(id,H,M,S);
in[i]=inPerson;
nodeoutPerson=node(id,H1,M1,S1);
out[i]=outPerson;
}
sort(in,in+m,cmpIn);
sort(out,out+m,cmpOut);
cout<
return0;
}
总结
⽤上结构体的构造函数来简化代码。
本文发布于:2022-11-27 13:23:44,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/90/31243.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |