华为笔试-到场嘉宾人数问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zh1204190329/article/details/82155509

12点到20点(分为8个整点时间区间)之间会有客人到来以及离去(到来和离去都看做整点,客人需在12点之后到来,20点之前离开),统计所有客人停留的时间长度(输入不多于100个客人的到达整点时间和离开整点时间,以“-1 -1”结束输入),最终输出每个时间区间客人的最大数目。

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <string.h>

using namespace std;

typedef struct _guesttime {
    int arrive;
    int leave;
} guestTime;

int main() {
    vector<guestTime> guest;
    guestTime tmp;
//  int end = 0;
//  char s[5];
    string s;
    cin >> s;
    cout << s[0] << endl;
    while (s[0] != '-') {
//      tmp.arrive = (s[0] - '0') * 10 + (s[1] - '0');
//      tmp.leave = (s[3] - '0') * 10 + (s[4] - '0');
        tmp.arrive = stoi(s.substr(s.find_first_not_of(",")));
        tmp.leave = stoi(s.substr(s.find_first_of(",")+1,2));
        guest.push_back(tmp);
        //  cout << guest.size() << endl;
        cin >> s;
    }

    // number of guests
    int num = guest.size();
    int cnt[21] = { 0 };
    for (int i = 0; i < num; i++) {
        int start = guest[i].arrive;
        int stay = guest[i].leave - guest[i].arrive;
        while (stay--) {
            cnt[start++]++;
        }
    }

    for (int i = 12; i < 20; i++) {
        cout << "[" << i << "," << i + 1 << "):" << cnt[i] << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/zh1204190329/article/details/82155509
今日推荐