• 1006. Sign In and Sign Out (25)


    题目连接:https://www.patest.cn/contests/pat-a-practise/1006

    原题如下:

    At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.

    Input Specification:

    Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

    ID_number Sign_in_time Sign_out_time
    

    where times are given in the format HH:MM:SS, and ID number is a string with no more than 15 characters.

    Output Specification:

    For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

    Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

    Sample Input:
    3
    CS301111 15:30:28 17:00:10
    SC3021234 08:00:00 11:25:25
    CS301133 21:45:00 21:58:40
    
    Sample Output:
    SC3021234 CS301133

    知道题目的核心是找出最小的时间和最大的时间以及相对应的序号
    我自己的写的是先读入,将信息存储在链表里,然后用strcmp()进行比较时间字符的大小,将最小和最大的时间对应的ID分别赋予变量,然后输出,思路简单但代码繁琐
    尤其是读入的操作,实在太繁琐了……
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 
     5 typedef struct SNode{
     6     char Sign_in_time[10];
     7     char Sign_out_time[10];
     8     char ID_number[20];
     9     struct SNode *Next;
    10 }Eeah;
    11 
    12 int main()
    13 {
    14     int M;
    15     char IDin[20],IDout[20];
    16     scanf("%d%*c",&M);
    17     Eeah *E=malloc(sizeof(struct SNode));
    18     E->Next=NULL;
    19     int i,j;
    20     int IDcnt=15,Incnt=9,Outcnt=9;
    21     int Tag=1;
    22     char ch;
    23     for (i=0;i<M;i++)
    24     {
    25         Eeah *Node=malloc(sizeof(struct SNode));
    26         Node->Next=E->Next;
    27 
    28         for (j=0;j<IDcnt;j++)
    29         {
    30             scanf("%c",&ch);
    31             if (ch!=' '&& Tag==1)
    32             {
    33                 Node->ID_number[j]=ch;
    34             }
    35             else if (ch==' ')
    36             {
    37                 Tag=2;
    38                 break;
    39             }
    40         }
    41 
    42         for (j=0;j<Incnt;j++)
    43         {
    44             scanf("%c",&ch);
    45             if (ch!=' '&&Tag==2)
    46             {
    47                 Node->Sign_in_time[j]=ch;
    48             }
    49             else if (ch==' ')
    50             {
    51                 Tag=3;
    52                 break;
    53             }
    54         }
    55 
    56         for (j=0;j<Outcnt;j++)
    57         {
    58             scanf("%c",&ch);
    59             if (ch!='
    '&& Tag==3)
    60             {
    61                 Node->Sign_out_time[j]=ch;
    62             }
    63             else if (ch=='
    ')break;
    64         }
    65 
    66         E->Next=Node;
    67         Tag=1;
    68     }
    69 
    70     Eeah *Tmp1=malloc(sizeof(struct SNode));
    71     Eeah *Tmp2=malloc(sizeof(struct SNode));
    72     Tmp1->Next=NULL;Tmp2->Next=NULL;
    73     Tmp1=E->Next;Tmp2=E->Next;
    74     strcpy(IDin,Tmp1->ID_number);strcpy(IDout,Tmp2->ID_number);
    75     E=E->Next;
    76 
    77     while (E->Next)
    78     {
    79         if (strcmp(Tmp1->Sign_in_time,E->Next->Sign_in_time)>0){strcpy(IDin,E->Next->ID_number);Tmp1=E->Next;}
    80         if (strcmp(E->Next->Sign_out_time,Tmp2->Sign_out_time)>0){strcpy(IDout,E->Next->ID_number);Tmp2=E->Next;}
    81         E=E->Next;
    82     }
    83 
    84    for (i=0;i<IDcnt;i++)
    85    {
    86        if ((IDin[i] >='0' && IDin[i]<='9')||(IDin[i]>='A' && IDin[i]<='Z'))printf("%c",IDin[i]);
    87 
    88        else break;
    89    }
    90    printf(" ");
    91     for (i=0;i<IDcnt;i++)
    92    {
    93        if ((IDout[i] >='0' && IDout[i]<='9')||(IDout[i]>='A' && IDout[i]<='Z'))printf("%c",IDout[i]);
    94        else break;
    95    }
    96    return 0;
    97 }
    View Code

     参考了其他网友的答案,输入极为简单,不用一个字符字符的输入判断,对于ID直接使用%s即可,因为碰到空格会断开;而对于时间的比较也不必使用字符串比较,利用%*c,只记录小时,分钟,秒的数值进行计算即可。并且不用将输入存储,一边进行输入,一边输出即可。

     1 #include<stdio.h>
     2 typedef struct node
     3 {
     4     int in_time;
     5     int out_time;
     6     char ID[20];
     7 }Node;
     8 
     9 int main()
    10 {
    11     int M,hour1,minute1,second1,h2,m2,s2;
    12     Node P_in,P_out,tmp;
    13     scanf("%d",&M);
    14 
    15     scanf("%s %d%*c%d%*c%d %d%*c%d%*c%d",&tmp.ID,&hour1,&minute1,&second1,&h2,&m2,&s2);
    16     tmp.in_time=3600*hour1+60*minute1+second1;
    17     tmp.out_time=3600*h2+60*m2+s2;
    18     P_in=P_out=tmp;
    19 
    20     M--;
    21     while(M--)
    22     {
    23         scanf("%s %d%*c%d%*c%d %d%*c%d%*c%d",&tmp.ID,&hour1,&minute1,&second1,&h2,&m2,&s2);
    24         tmp.in_time=3600*hour1+60*minute1+second1;
    25         tmp.out_time=3600*h2+60*m2+s2;
    26         if (tmp.in_time<P_in.in_time)P_in=tmp;
    27         if (tmp.out_time>P_out.out_time)P_out=tmp;
    28     }
    29 
    30     printf("%s %s",P_in.ID,P_out.ID);
    31 }
    View Code






  • 相关阅读:
    【转】extern "C"的含义和用法
    python的shelve库
    【转】TCP是流传输协议,UDP是包传输协议
    【转】TCP慢启动、拥塞避免、快速重传、快速恢复
    【转】C/C++多线程编程中什么情况下需要加volatile?
    【转】C++对象是创建在堆上,还是在栈上?
    【转】腾讯高级工程师:一道面试题引发的高并发性能调试思考
    【转】C++类的sizeof大小
    【转】C编译器内存对齐
    【转】c++中使用memset初始化类对象
  • 原文地址:https://www.cnblogs.com/wuxiaotianC/p/6347525.html
Copyright © 2020-2023  润新知