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

10 篇文章 0 订阅

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值