我是靠谱客的博主 敏感龙猫,最近开发中收集的这篇文章主要介绍PAT 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

题目大意

第一行给出一个数字,是每天共有几个人进入机房。
剩下几行每一行都是一个登陆者的ID,登陆时间和登出时间。
题目要求找出每天第一个登出和最后一个登录的人。

代码思路

先贴代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
typedef struct n {
string id;
string in, out;
}Node;
vector<Node> ID;
bool cmp1(Node a, Node b) {
return a.in < b.in;
}
bool cmp2(Node a, Node b) {
return a.out > b.out;
}
int main() {
int n;
string _id, _in, _out;
Node temp;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cin >> _id >> _in >> _out;
temp.id = _id;
temp.in = _in;
temp.out = _out;
ID.push_back(temp);
}
sort(ID.begin(), ID.end(), cmp1);
cout << ID[0].id;
printf(" ");
sort(ID.begin(), ID.end(), cmp2);
cout << ID[0].id;
return 0;
}

思路

整个题目来讲,如果单纯的用C,个人认为难度会高很多,输入中全部都是字符串,而且要对字符串根据字典序比较,还要根据结构体中的某一项进行正序反序排序。同时对于多个连续字符串读入,处理起来也很麻烦。
如果使用STL的话,题目难度就很低了,对题干直接进行翻译即可。
注意读入的时候因为使用了动态数组,赋值那里要格外注意。可以定义一个临时变量作为中介读入到数组中,也可使用构造函数。
后面的话就是运用sort函数,sort函数排序时间复杂度要比自己手撸出来的时间复杂度优秀,所以与其想很久coding不如直接使用已有的库函数。

最后

以上就是敏感龙猫为你收集整理的PAT 1006题目代码思路的全部内容,希望文章能够帮你解决PAT 1006题目代码思路所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部