原题目 https://pintia.cn/problem-sets/994805342720868352/problems/994805516654460928
解决问题的代码:
#include<stdio.h>
#include<string.h>
/*
样例输入
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
结果:
SC3021234 CS301133
*/
int main()
{
int num = 0;
scanf("%d", &num);
char ID[16] = "";
char firstID[16] = "";
char lastID[16] = "";
int signInTime = -1;
int signOutTime = -1;
int firstTime = 24 * 3600;
int lastTime = 0;
int i = 0;
for(; i < num; i++)
{
int h1 = 0;
int m1 = 0;
int s1 = 0;
int h2 = 0;
int m2 = 0;
int s2 = 0;
scanf("%s %d:%d:%d %d:%d:%d", &ID, &h1, &m1, &s1, &h2, &m2, &s2);
signInTime = h1 * 3600 + m1 * 60 + s1;
signOutTime = h2 * 3600 + m2 * 60 + s2;
if(signInTime < firstTime)
{
strcpy(firstID, ID);
firstTime = signInTime;
}
if(signOutTime > lastTime)
{
strcpy(lastID, ID);
lastTime = signOutTime;
}
}
printf("%s %s", firstID, lastID);
return 0;
}
这道题注意两个要点:
1.时间转化为秒存储
2.数组长度不是题目说的15而至少应该是16
特别是第2点最坑,要不是我灵光一闪想起来字符串最少存储的长度应该是字符长度+1我估计还在以头撞墙。哎,赶紧记下来。