代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct member
{
char id[20];
char stime[20];
char otime[20];
};
int main()
{
int totalCount;
int earliest = 0;
int latest = 0;
printf("请输入人数,要求小于20\n");
scanf("%d", &totalCount);
printf("\n试验室总人数为:%d\n", totalCount);
struct member *didi = (struct member*)malloc(sizeof(struct member) * totalCount); //动态创建结构体数组
for (int i = 0; i < totalCount; i++)
{
printf("请输入第%d个人的id和进出时间:\n",i+1);
scanf("%s %s %s", (didi+ i)->id, (didi + i)->stime, (didi + i)->otime);
}
for (int i = 0; i < totalCount - 1; i++)
{
if (strcmp((didi + i + 1)->stime, (didi + i)->stime ) < 0)
{
earliest = i + 1;
}
if (strcmp((didi + i + 1)->stime, (didi + i)->stime) > 0)
{
latest = i + 1;
}
printf("\n");
}
printf("Open: %s %s \n", (didi + earliest)->id, (didi + earliest)->stime);
printf("Close: %s %s \n", (didi + latest)->id, (didi + latest)->otime);
}
代码不算复杂,因为试验室人数不定,所以需要动态创建结构体数组,以及指针操作。
测试结果如下:
用心回答每个问题,如果对您有帮助,请采纳答案好吗,谢谢。