
各位知道咋做嘛?快来帮帮我呀!有哪位给我讲讲思路,或写个注释
示例如下:
#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的领先总时间。
注:这里假设输入的数据是按时间顺序的
希望能帮到你,加油~~~