L2-034 口罩发放
破防了,我自己写的只能得5分,测试点0都过不去,并且至今没有找到错误的原因。
等我找到了再回来。
AC了回来了。
我的思路没问题,单纯是代码写错了。
错误1: 结构体中的id错误地使用了string类型,我真的服了!!!所以4,5过不去,排序就错了。
错误2: 在一天开始之前,限制天数-1.错误的将这部分代码写到了第二层循环,位置写错了。
(正确版和错误版全放下面了,以史为鉴)
(测试点45过不去肯定是100%排序出问题了,检查排序)
正确版本:
#include <bits/stdc++.h> using namespace std; struct node { int id; string tname; string tno; int status; int h, m; }ss[30010]; map<string, node> person;//记录所有人的信息 map<string, int> mp;//身份证号 发放时间 vector<string> record;//记录不健康的人 bool cmp(node n1, node n2) { //提交时间 if (n1.h != n2.h) { return n1.h < n2.h; } if (n1.m != n2.m) { return n1.m < n2.m; } return n1.id < n2.id; } void print(node n) { printf("%s %s\n", n.tname.c_str(), n.tno.c_str()); } bool check(string str) { if (str.size() != 18) return false; for (int i = 0; i < 18; i++) { if (!isdigit(str[i])) return false; } return true; } int main() { int d, p; cin >> d >> p; while (d--) {//第几天 int t, s;//t记录数量 s口罩数量 cin >> t >> s; int cnt = 0;//有效的人数 for (auto x : mp) { if (x.second > 0) { mp[x.first] = x.second - 1; } } for (int i = 0; i < t; i++) { //是否满足需求 string tname, tno; int status, h, m; cin >> tname >> tno >> status >> h; cin.get(); cin >> m; if (check(tno)) {//身份证号是18位 if (mp[tno] <= 0) {//在限制时间范围外有资格领取口罩 //加入其中 ss[cnt].tname = tname; ss[cnt].tno = tno; ss[cnt].status = status; ss[cnt].h = h; ss[cnt].m = m; ss[cnt].id = cnt; person[tno] = ss[cnt]; cnt++; } if (status == 1) {//拥有合法记录 并且身体状态是1 保存其身份证号 if (find(record.begin(), record.end(), tno) == record.end()) { record.push_back(tno); } } } } //将符合要求的人进行排序 sort(ss, ss + cnt, cmp); for (int j = 0; j < cnt; j++) { if (s == 0) break; if (mp[ss[j].tno] <= 0) { mp[ss[j].tno] = p + 1;//发了口罩 设置时间 print(ss[j]); s--; } } } //打印status=1的人按照次序 for (auto x : record) {//x是账号 3 2 1 print(person[x]); } return 0; }
错误版本:
#include <bits/stdc++.h> using namespace std; struct node { string id; string tname; string tno; int status; int h, m; }ss[30010]; map<string, node> person;//记录所有人的信息 map<string, int> mp;//身份证号 发放时间 vector<string> record;//记录不健康的人 bool cmp(node n1, node n2) { //提交时间 if (n1.h != n2.h) { return n1.h < n2.h; } if (n1.m != n2.m) { return n1.m < n2.m; } return n1.id < n2.id; } void print(node n) { printf("%s %s\n", n.tname.c_str(), n.tno.c_str()); } bool check(string str) { if (str.size() != 18) return false; for (int i = 0; i < 18; i++) { if (!isdigit(str[i])) return false; } return true; } int main() { int d, p; cin >> d >> p; while (d--) { int t, s;//t记录数量 s口罩数量 cin >> t >> s; int cnt = 0;//该天满足要求的人数 for (int i = 0; i < t; i++) { for (auto x : mp) { if (x.second > 0) { mp[x.first] = x.second - 1; } } //是否满足需求 string tname, tno; int status, h, m; cin >> tname >> tno >> status >> h; cin.get(); cin >> m; if (check(tno)) {//身份证号是18位 if (mp[tno] <= 0) {//在限制时间范围外有资格领取口罩 //加入其中 ss[cnt].tname = tname; ss[cnt].tno = tno; ss[cnt].status = status; ss[cnt].h = h; ss[cnt].m = m; ss[cnt].id = cnt; person[tno] = ss[cnt]; cnt++; } if (status == 1) {//拥有合法记录 并且身体状态是1 保存其身份证号 if (find(record.begin(), record.end(), tno) == record.end()) { record.push_back(tno); } } } } //将符合要求的人进行排序 sort(ss, ss + cnt, cmp); for (int j = 0; j < cnt; j++) { if (mp[ss[j].tno] <= 0) { mp[ss[j].tno] = p + 1;//发了口罩 设置时间 print(ss[j]); s--; if (s == 0) break; } } } //打印status=1的人按照次序 for (auto x : record) {//x是账号 print(person[x]); } return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步