zoukankan      html  css  js  c++  java
  • PAT A1006 Sign In and Sign Out

    本程序为PAT A1006 Sign In and Sign Out答案,题目链接

    主体思想:建立一个结构体,存储ID账号,Sign_in时间,Sign_out时间。

    详细介绍:

      由于时间的输入格式为HH:MM:SS,先将之存为字符串string类型,在依次读取出HH,MM,SS,然后将其转化为整数形式进行加和,加和结果为HHMMSS。这样做的目的时,可以通过比较整数形HHMMSS的大小直接比较出其时间的先后关系。

      将时间格式转换完成之后的工作就非常简单了,我们只需要寻找出Sign_in时间的最小值与Sign_out时间的最大值,依次输出其对应的ID账号即可。

    注意:

      暂无

    程序代码:

     1 #include "pch.h"
     2 #include <iostream>
     3 #include <string>
     4 using namespace std;
     5 
     6 struct inf {
     7     string ID_number;
     8     int Sign_in_time;
     9     int Sign_out_time;
    10 };
    11 int main()
    12 {
    13     inf f[500];
    14     int M;
    15     cin >> M;
    16     string temp1, temp2;
    17     for (int i = 0; i < M; ++i) {
    18         cin >> f[i].ID_number >> temp1 >> temp2;
    19         f[i].Sign_in_time = (temp1[0] - '0') * 100000 + (temp1[1] - '0') * 10000 + (temp1[3] - '0') * 1000 
    20             + (temp1[4] - '0') * 100 + (temp1[6] - '0') * 10 + (temp1[7] - '0');
    21         f[i].Sign_out_time= (temp2[0] - '0') * 100000 + (temp2[1] - '0') * 10000 + (temp2[3] - '0') * 1000
    22             + (temp2[4] - '0') * 100 + (temp2[6] - '0') * 10 + (temp2[7] - '0');
    23     }
    24     int max_in = f[0].Sign_in_time, max_out = f[0].Sign_out_time;
    25     int first_in=0, last_out=0;
    26     for (int i = 1; i < M; ++i) {
    27         if (f[i].Sign_in_time < max_in) {
    28             max_in = f[i].Sign_in_time;
    29             first_in = i;
    30         }
    31         if (f[i].Sign_out_time > max_out) {
    32             max_out = f[i].Sign_out_time;
    33             last_out = i;
    34         }
    35     }
    36     cout << f[first_in].ID_number << ' ' << f[last_out].ID_number << endl;
    37 }

    提交结果:

  • 相关阅读:
    [笔迹]java范型
    转:APNS设置
    orientation in a UIView add to a UIWindow
    no password for ssh
    webservice soap
    set the region for all annotation
    iOS与Java服务器GZip压缩问题【转】
    useful commands for Mac / iOS
    textView使用总结
    总结(不断更新)
  • 原文地址:https://www.cnblogs.com/qujunhui/p/10908785.html
Copyright © 2011-2022 走看看