如何做的网站排第一/网页设计期末作业模板
L2-034 口罩发放 - 团体程序设计天梯赛-练习集 (pintia.cn)
题解
1、数据结构定义
(1)People
结构体,用于存储申请人的各项信息
struct People{ int sx; // 申请顺序 string name; // 姓名 string id; // 身份证号 int flag; // 身体情况 int h; // 时针 int m; // 分针 int day; // 申请成功的那天 };
(2)map<string, int> lastSuccessDay
来记录合法身份证的人申请到口罩的日期,便于判断是否满足间隔要求。
(3)vector<People> sickPeople
记录有合法记录且身体状况为 1 的申请人。
2、数据读取
-
读取天数
D
和间隔天数P
。 -
针对每一天的数据,先读取当天的申请数量
T
和口罩发放名额S
。 -
依次读取每条申请信息,若身份证号合法,就将其存入
peo
向量;若申请人身体状况为 1 且未在sickPeople
中出现过,就将其添加到sickPeople
中。
3、数据筛选与排序:
-
借助
is_valid_id
函数判断身份证号是否为 18 位数字。 -
对当天的申请信息按提交时间排序,若提交时间相同,则按申请顺序排序。
4、口罩发放:
-
遍历排序后的申请信息,若申请人未申请过口罩或者距离上次申请超过
P
天,就发放口罩,输出其姓名和身份证号,并更新lastSuccessDay
。
5、结果输出:
-
输出每一天的口罩发放记录。
-
输出有合法记录且身体状况为 1 的申请人的姓名和身份证号。
代码
#include<bits/stdc++.h>
using namespace std;
struct People{int sx; // 申请顺序string name; // 姓名string id; // 身份证号int flag; // 身体情况int h; // 时针int m; // 分针int day; // 申请成功的那天
};
//排序
bool cmp(People a, People b){if(a.h != b.h){return a.h < b.h;}else if(a.m != b.m){return a.m < b.m;}return a.sx < b.sx;
}
//判断是否是 18 位的数字
bool is_valid_id(string id){if(id.size() != 18) return false;for(char c : id){if(!isdigit(c)) return false;}return true;
}
int main(){int d, p;cin >> d >> p;map<string, int> lastSuccessDay; //记录合法身份证的人的id和他们申请到的口罩的日期vector<People> sickPeople;//记录有合法记录的、身体状况为 1 的申请人的姓名及身份证号for(int i = 1; i <= d; i++){int t, s;cin >> t >> s;vector<People> peo;for(int j = 0; j < t; j++){string a, b;int x, y, z;cin >> a >> b >> x;scanf("%d:%d", &y, &z);if(is_valid_id(b)){peo.push_back({j, a, b, x, y, z, 0});if(x == 1){bool found = false;for(auto& person : sickPeople){if(person.id == b){found = true;break;}}if(!found){sickPeople.push_back({j, a, b, x, y, z, 0});}}}}sort(peo.begin(), peo.end(), cmp);int given = 0;for(int j = 0; j < peo.size() && given < s; j++){if(lastSuccessDay.find(peo[j].id) == lastSuccessDay.end() || i - lastSuccessDay[peo[j].id] > p){cout << peo[j].name << " " << peo[j].id << endl;lastSuccessDay[peo[j].id] = i;given++;}}}for(auto& person : sickPeople){cout << person.name << " " << person.id << endl;}return 0;
}