题解 | 游船出租 简单粗暴模拟 不用栈

游船出租

https://www.nowcoder.com/practice/50fc5c0009cf4083bca5fbedcb4b6dc0

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
struct boat {
    int start_time_h;
    int start_time_m;
    int end_time;
    bool isLend = false;

};
int main() {
    vector<boat> vec1(101);
    int sum_h = 0;
    int sum_m = 0;
    int id;
    char staus;
    string str1;
    int count = 0;
    while (getline(cin, str1)) {
        if (str1[0] == '-')break;
        if (str1[0] == '0'&&count!=0) {
            printf("%d %.0f\n", count, ((float)sum_m + 60 * (float)sum_h)/(float)count);
            sum_h = 0;
            sum_m = 0;
            count = 0;
        }else if(str1[0] == '0') {
            printf("0 0\n");
        }
        if (str1[1] == ' ') {
            id = str1[0] - '0';
            str1 = str1.substr(2);
        } else if (str1[2] == ' ') {
            id = str1[0] - '0';
            id = id * 10 + str1[1] - '0';
            str1 = str1.substr(3);
        } else {
            id = 100;
            str1 = str1.substr(4);
        }
        staus = str1[0];
        str1 = str1.substr(2);
        if (staus == 'E' && vec1[id].isLend == false) {
            continue;
        } else if (staus == 'S') {
            vec1[id].isLend = true;
            vec1[id].start_time_h = stoi(str1.substr(0, 2));
            vec1[id].start_time_m = stoi(str1.substr(3));
            
        } else {
            sum_h +=  stoi(str1.substr(0, 2)) - vec1[id].start_time_h ;
            sum_m +=  stoi(str1.substr(3,2)) - vec1[id].start_time_m ;
            // printf("--%d %d\n",stoi(str1.substr(0, 2)) - vec1[id].start_time_h,stoi(str1.substr(3,2)) - vec1[id].start_time_m);
            count++;
        }
//      printf("---%d %c %s\n",id,staus,str1.c_str());
    }



    return 0;
}

全部评论

相关推荐

xxxxOxo:该催就催,想要你的不会因为催就挂,催了就挂的是因为本来就要挂你
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务