华为2019校招笔试题及解法2

题目

题目来源于网上:
华为2019校招笔试题及解法2_第1张图片

思路

建一个8x2的数组,一行表示一个时间段,第一列表示该时间段进来的人数,第二列表示该时间段离开的人数。
某个时间段的人数 = 该时间段进来的人数 + 上个时间段的人数 - 上个时间段离开的人数。有点动态规划的思想。

代码

#include
#include
#include

using namespace std;
vector> dat(8,vector{0,0});
int main()
{
    int i,j;
    while(1)
    {
        cin >> i >> j;
        if(i==-1 && j==-1)
            break;
        dat[i-12][0]++;
        dat[j-13][1]++;
    }
    vector cnt(8,0);
    cnt[0] = dat[0][0];
    cout << "[12,13):"<< cnt[0] << endl;
    for(i=1;i<8;i++)
    {
        cnt[i] = dat[i][0] + cnt[i-1] - dat[i-1][1];
        cout << "["<

华为2019校招笔试题及解法2_第2张图片

总结

我是菜鸡。

你可能感兴趣的:(刷题吧)