网易2019校招笔试,今日头条2019校招笔试(前两道)

网易2019校招笔试题

1. 丰收

题目:

就是堆了几堆的南瓜,然后给出几个数字作为序号,猜是第几堆里面的南瓜

思路:

思路不难,就是进行累加,然后找出数字在第几个堆里。让人不爽的是时间复杂度败给了调用函数的,就是这个lower_bound(first *itor,last *itor,const int T)函数,它指向在[first,last]标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个不小于value 的值

代码:

#include <bits/stdc++.h>

using namespace std;

int sum[100005];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        int a;
        scanf("%d", &a);
        sum[i] = sum[i - 1] + a;
    }
    int q;
    scanf("%d", &q);
    while (q--) {
        int d;
        scanf("%d", &d);
        int pos = lower_bound(sum, sum + n + 1, d) - sum;
        printf("%d\n", pos);
    }
    return 0;
}

2. 瞌睡

题目:

就是一个傻逼小孩上课打瞌睡,然后还想让同桌叫醒他,让他再学那么几分钟,让我们求这个傻逼小孩上课收益最大(能听到课内容最多)

思路:

思路不难,就是滑动窗口的动态规划,这个听了好多次,但是没有务实地学会。

代码:

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n), t(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int now = 0;
    for (int i = 0; i < n; i++)
        cin >> t[i], now += t[i] * a[i];
    int res = now;
    for (int i = 0; i < n;) {
        now += (!t[i]) * a[i];
        if (++i >= k) {
            res = max(res, now);
            now -= (!t[i - k]) * a[i - k];
        }
    }
    cout << res << endl;
    return 0;
}

今日头条2019校招笔试题

1. 求矩形的最大连通域

题目:

球场团队数与最大团队的人数

思路:

bfs,参考leetcode 200. Number of Islands。要注意的是输入,用逗号隔开,解决方法是每次读取一个数后,都调用cin.get()来清除逗号

代码:

void bfs(vector<vector<int>>& grid, int i, int j, int& num)

{

    if (i<0 || i>grid.size() - 1 || j<0 || j>grid[0].size() - 1 || grid[i][j] == 0)

        return;

    grid[i][j] = 0;

    ++num;

    bfs(grid, i - 1, j, num);

    bfs(grid, i + 1, j, num);

    bfs(grid, i, j - 1, num);

    bfs(grid, i, j + 1, num);

    bfs(grid, i - 1, j - 1, num);

    bfs(grid, i - 1, j + 1, num);

    bfs(grid, i + 1, j - 1, num);

    bfs(grid, i + 1, j + 1, num);

    return;

}


int main()

{

    int M, N;

    cin >> M;

    cin.get();

    cin >> N;

    vector<vector<int> > grid(M, vector<int>(N, 0));

    for (int i = 0; i < M; ++i)

    {

        for (int j = 0; j < N; ++j)

        {

            cin >> grid[i][j];

            cin.get();

        }

    }


    int num = 0, maxNum = 0;

    for (int i = 0; i != M; ++i)

    {

        for (int j = 0; j != N; ++j)

            if (grid[i][j] == 1)

            {

                int tmpMax = 0;

                bfs(grid, i, j, tmpMax);

                ++num;

                if (tmpMax > maxNum)

                    maxNum = tmpMax;

            }

    }

    cout << num << "," << maxNum << endl;

    return 0;

}

2. 区间合并

题目:

有多人给出区间,每个人给出多个,要求合并

思路:

先排序,排序规则是先左端点升序,相同时右端点升序。leetcode好像有做过类似的。。ps:这道题的分号逗号有点恶心,要单独处理下。

代码:

#include <string>

#include <vector>

#include <algorithm>

#include <iostream>


using namespace std;


struct Interval {

    long long start;

    long long end;

    Interval() : start(0), end(0) {}

    Interval(long long s, long long e) : start(s), end(e) {}

};


bool com(const Interval& a, const Interval& b)

{

    if (a.start != b.start)

        return a.start < b.start;

    if (a.end != b.end)

        return a.end < b.end;

    return false;

}


void SplitString(const string& s, vector<string>& v, const string& c)

{

    string::size_type pos1, pos2;

    pos2 = s.find(c);

    pos1 = 0;

    while (string::npos != pos2)

    {

        v.push_back(s.substr(pos1, pos2 - pos1));


        pos1 = pos2 + c.size();

        pos2 = s.find(c, pos1);

    }

    if (pos1 != s.length())

        v.push_back(s.substr(pos1));

}


int main()

{

    int m;

    cin >> m;


    vector<Interval> intervals;

    for (int i = 0; i < m; ++i)

    {

        string s;

        cin >> s;

        vector<string> v;

        SplitString(s, v, ";");

        int num = v.size();


        for (int j = 0; j < num; ++j)

        {

            vector<string> v0;

            SplitString(v[j], v0, ",");

            Interval x;

            x.start = stoi(v0[0]);

            x.end = stoi(v0[1]);

            intervals.push_back(x);

        }

    }


    sort(intervals.begin(), intervals.end(), com);

    long long len = intervals.size();

    vector<Interval> res;

    long long start(0);

    long long end(0);


    for (long long i = 0; i < len;)

    {

        start = intervals[i].start;

        end = intervals[i].end;

        long long j = i + 1;

        for (; j < len; ++j)

        {

            if (end < intervals[j].start)

                break;

            end = max(end, intervals[j].end);

        }

        res.push_back(Interval(start, end));

        i = j;

    }


    long long len2 = res.size();

    for (long long i = 0; i < len2; ++i)

    {

        cout << res[i].start << ',' << res[i].end;

        if (i != len2 - 1)

            cout << ';';

    }

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011268787/article/details/81591883
文章标签: 网易笔试 C++
个人分类: 找工作
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭