EternalLBZ 2024-01-23 19:45 采纳率: 7.7%
浏览 13

nbdp 1494 Problem D 三、篮球比赛

img

各位知道咋做嘛?快来帮帮我呀!有哪位给我讲讲思路,或写个注释

  • 写回答

1条回答 默认 最新

  • GameTomato 2024-01-24 11:05
    关注

    示例如下:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    struct Goal {
        int team;
        int minutes;
        int seconds;
    };
    
    int main() {
        int n;
        cin >> n;
    
        vector<Goal> goals;
    
        for (int i = 0; i < n; ++i) {
            Goal goal;
            cin >> goal.team >> goal.minutes;
            getchar(); // consume ':'
            cin >> goal.seconds;
            goals.push_back(goal);
        }
    
        // 排序,按照时间顺序
        sort(goals.begin(), goals.end(), [](const Goal &a, const Goal &b) {
            return a.minutes < b.minutes || (a.minutes == b.minutes && a.seconds < b.seconds);
        });
    
        int team1Time = 0, team2Time = 0;
        int lastMinute = 0, lastSecond = 0;
    
        for (const Goal &goal : goals) {
            int goalTime = goal.minutes * 60 + goal.seconds;
            int deltaTime = goalTime - (lastMinute * 60 + lastSecond);
    
            if (goal.team == 1) {
                team1Time += deltaTime;
            } else {
                team2Time += deltaTime;
            }
    
            lastMinute = goal.minutes;
            lastSecond = goal.seconds;
        }
    
        cout << "队伍1领先总时间:" << team1Time / 60 << "分:" << team1Time % 60 << "秒" << endl;
        cout << "队伍2领先总时间:" << team2Time / 60 << "分:" << team2Time % 60 << "秒" << endl;
    
        return 0;
    }
    

    首先读取进球数据,然后按时间排序。接着,遍历排序后的进球数据,计算每个进球时两队的领先时间,并统计总的领先时间。最后输出队伍1和队伍2的领先总时间。

    注:这里假设输入的数据是按时间顺序的

    希望能帮到你,加油~~~

    评论

报告相同问题?

问题事件

  • 创建了问题 1月23日