本文主要是介绍浙大PAT (Advanced Level) Practise 1006,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//本题是一道比较简单的排序题,建立一个结构体并用容器vector存储每条记录,以algorithm排序,获取开门人和关门人id。
//代码如下:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Person //定义一个记录的数据结构
{string id;string in;string out;
};bool cmp1(Person a,Person b)
{return a.in<b.in;
}
bool cmp2(Person a,Person b)
{return a.out>b.out;
}int main()
{int n;while(cin>>n){vector<Person> day;Person buff;for(int i=0;i<n;++i){cin>>buff.id>>buff.in>>buff.out;day.push_back(buff);}sort(day.begin(),day.end(),cmp1); //根据每个人进入的时间从早到晚排序cout<<day[0].id<<' '; //输出最早进入,即开门人的idsort(day.begin(),day.end(),cmp2); //根据每个人进入的时间从晚到早排序cout<<day[0].id<<endl; //输出锁门人}return 0;
}
这篇关于浙大PAT (Advanced Level) Practise 1006的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!