L1-043 阅览室 map 方法

#include <iostream>
#include <vector>
#include <map>
using namespace std;
struct node
{
    char c;
    int time;
};
int main()
{
    int N;
    cin >> N;
    while (N--)
    {
        int sum = 0;
        int cnt = 0;
        //序号做键 操作和时间作为值
        map<int, node> mp;
        while (1)
        {
            int a, c, d;
            char b;
            scanf("%d %c%d:%d", &a, &b, &c, &d);
            //一天的结束
            if (a == 0)
                break;
            //计算时间
            c = c * 60 + d;
            //mp.count(键) 查看map里有几个键为a的
            //其实 只返回0和1 因为map没有重复的
            //可以当作查看有没有这个键值来用
            if (mp.count(a) == 1 && mp[a].c == 'S' && b == 'E')
            {
                cnt++;
                sum += c - mp[a].time;
                //记得要删除 不然测试点1过不去 比如这个数据
                //1
                //1 S 9:50
                // 2 S 08:35
                // 1 E 10:00
                // 1 E 10:10
                // 0 S 17:00
                mp.erase(a);
            }
            else
                mp[a] = {b, c};
        }
        //                                  四舍五入
        cnt == 0 ? printf("0 0\n") : printf("%d %.0f\n", cnt, 1.0 * sum / cnt);
    }
    return 0;
}

发布了116 篇原创文章 · 获赞 27 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45653525/article/details/104700704