题解 | 游船出租

游船出租

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;
}

全部评论

相关推荐

02-28 01:18
已编辑
南昌大学 后端工程师
黑皮白袜臭脚体育生:把开源经历放个人项目上边应该更好,就像大部分人都把实习经历放个人项目上边
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务