题解 | 游船出租
游船出租
https://www.nowcoder.com/practice/50fc5c0009cf4083bca5fbedcb4b6dc0
#include <iostream>
#include <string>
using namespace std;
#include <cstdio>
#include <stack>
#include <vector>
#include <map>
int main() {
int num; //船号
char c; //开始结束标志
string time;
map<int, string> mymap;
int totalTime = 0;
double totalNum = 0;
while (cin >> num >> c >> time && num != -1) {
if (c == 'S') {
mymap[num] = time; //记录租船
} else if (c == 'E' && mymap.count(num)) {
string ss1 = mymap[num]; //当前归还船只
totalTime += (stoi(time.substr(0, 2)) - stoi(ss1.substr(0,2))) * 60
+ (stoi(time.substr(3)) - stoi(ss1.substr(3))); //如果当前船只是归还的,那么总时间加上
totalNum++; //租船次数加1,归还了才算
}
if (num == 0) {
double ave = totalTime /
totalNum; //计算平均时间,注意避免精度错误,要用double
printf("%.0lf %.0lf", totalNum, ave);
cout << endl;
totalNum = 0;
totalTime = 0;
}
}
return 0;
}

查看16道真题和解析