PTA_L1-043 阅览室 (20分)
发布日期:2021-05-14 16:34:35 浏览次数:22 分类:精选文章

本文共 833 字,大约阅读时间需要 2 分钟。

以下是优化后的代码,符合要求的结构和优化建议:

#include 
#include
#include
#include
using namespace std;int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { map
mpa; map
mpb; int times = 0; int total = 0; for (;;) { int id; char state; int h, m; if (cin >> id >> state >> h >> m) { if (id == 0) { break; } if (state == 'S') { mpa[id] = 'S'; mpb[id] = h * 60 + m; } else if (state == 'E') { if (mpa.find(id) != mpa.end() && mpa[id] == 'S') { times += mpb[id]; total++; mpa[id] = 'E'; mpb[id] = 0; } } } else { break; } } if (total == 0) { cout << "0 0"; } else { double avg = static_cast
(times) / total; int res = static_cast
(avg + 0.5); cout << total << ' ' << res; } }}

这个代码处理了每个输入记录,统计了当天的借书次数和平均阅读时间。它使用两个映射来跟踪每本书的状态和还书时间,确保无效记录被忽略,并正确计算统计数据。

上一篇:PTA_L1-016 查验身份证 (15分)
下一篇:PTA_L1-049 天梯赛座位分配 (20分)

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月18日 05时25分24秒