今日头条2018秋招笔试题(未完待续)

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

第一题:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
void helper(const vector<vector<int>>& array, vector<vector<bool>>& check, int& cnt, int i, int j) {
    if (i < 0 || i >= array.size() || j < 0 || j >= array[0].size()) {
        return;
    }
    if (array[i][j] == 1 && !check[i][j]) {
        ++cnt;
        check[i][j] = true;
        helper(array, check, cnt, i-1, j-1);
        helper(array, check, cnt, i-1, j);
        helper(array, check, cnt, i-1, j+1);
        helper(array, check, cnt, i, j-1);
        helper(array, check, cnt, i, j+1);
        helper(array, check, cnt, i+1, j-1);
        helper(array, check, cnt, i+1, j);
        helper(array, check, cnt, i+1, j+1);
    } else {
        return;
    }
}
int main() {
    int M, N;
    char b;
    cin >> M >> b >> N;
    vector<vector<int>> array(M, vector<int>(N, 0));
    vector<vector<bool>> check(M, vector<bool>(N, false));
    int tmp;
    int i, j;
    for (i = 0; i < M; ++i) {
        for (j = 0; j < N - 1; ++j) {
            cin >> tmp;
            cin >> b;
            array[i][j] = tmp;
        }
        if (j == N - 1) {
            cin >> tmp;
            array[i][j] = tmp;
        }
    }
    int P = 0;
    int Q = 0;
    int cnt = 0;
    for (int i = 0; i < M; ++i) {
        for (int j = 0; j < N; ++j) {
            if (array[i][j] == 1 && !check[i][j]) {
                helper(array, check, cnt, i, j);
                Q = max(Q, cnt);
                cnt = 0;
                ++P;
            }
        }
    }
    cout << P << "," << Q << endl;
}

第一题主要考了递归调用,但这个带逗号的输入确实是个小陷阱,要换一种输入读取方法。

 

第二题:

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
int main() {
    int T;
    cin >> T;
    vector<pair<int, int>> array;
    pair<int, int> tmp;
    string s;
    for (int i = 0; i < T+1; ++i) {
        getline(cin, s);
        istringstream is (s);
        int inter1, inter2;
        char ch;
        while (is >> inter1 >> ch >> inter2)
        {
            tmp.first = inter1;
            tmp.second = inter2;
            array.push_back(tmp);
            is >> ch;
        }
    }
    if (array.size() == 0) {
        return 0;
    }

    sort(array.begin(), array.end());

    vector<pair<int, int>> res;
    int a1 = array[0].first;
    int b1 = array[0].second;

    for (int i = 1; i < array.size(); ++i) {
        int a = array[i].first;
        int b = array[i].second;
        if (a <= b1) {
            b1 = max(b1, b);
        } else {
            res.push_back(pair<int, int>(a1, b1));
            a1 = a;
            b1 = b;
        }
    }
    res.push_back(pair<int ,int>(a1, b1));

    for (int i = 0; i < res.size() - 1; ++i) {
        cout << res[i].first << "," << res[i].second << ";";
    }
    cout << res[res.size() - 1].first << "," << res[res.size() - 1].second;
    return 0;
}

第二题在leetcode上有原题,只需要先对pair<int, int>做一个排序,之后的处理就很自然了;在输入输出格式上依然需要注意。

 

第三题:

不会.................

 

第四题:

思路:用dp的方式,将a/b 在空间i到j的区间的最大值/最小值求出来,再逐个空间进行比较;笔试没写完

 

第五题:

来不及看题...........

阅读更多
想对作者说点什么? 我来说一句

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