寒武纪 9.29 算法笔试

问一句还有hc吗。。题不算太难。
1
// 判断n正多边形是否存在垂直边
#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main() {
    int q; cin >> q;
    while (q--) {
        string t; cin >> t;
        if (t.size() <= 2) {
            if (stoi(t) % 4 == 0) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
        else {
            string tem;
            tem += t[t.size() - 2];
            tem += t[t.size() - 1];
            if (stoi(tem) % 4 == 0) cout << "YES" << endl;
            else cout << "NO" << endl;
        }

    }
    return 0;
}
2
// 找全排列中的子排列
#include<iostream>
#include<vector>
using namespace std;
const int N = 1e7;
int L[N];
int main() {
    int t; cin >> t;
    int n;
    while (t--) {
        //scanf("%d", &n);
        cin >> n;
        int s;
        for (int i = 0; i < n; i++) {
            //scanf("%d", &L[i]);
            cin >> L[i];
            if (L[i] == 1) s = i;
        }
        vector<int> ans(n, 0);
        int l = s - 1, r = s + 1;
        vector<int> tem;
        int maxt = 0;
        maxt = max(maxt, L[s]);
        tem.push_back(L[s]);
        if (maxt == tem.size()) ans[tem.size() - 1] = 1;
        while (l >= 0 || r < n) {
            if (l < 0) {
                tem.push_back(L[r]);
                maxt = max(maxt, L[r]);
                if (maxt == tem.size()) ans[tem.size() - 1] = 1;
                r++;
            }
            else if (r >= n) {
                tem.push_back(L[l]);
                maxt = max(maxt, L[l]);
                if (maxt == tem.size()) ans[tem.size() - 1] = 1;
                l--;
            }
            else {
                if (L[l] < L[r]) {
                    tem.push_back(L[l]);
                    maxt = max(maxt, L[l]);
                    if (maxt == tem.size()) ans[tem.size() - 1] = 1;
                    l--;
                }
                else {
                    tem.push_back(L[r]);
                    maxt = max(maxt, L[r]);
                    if (maxt == tem.size()) ans[tem.size() - 1] = 1;
                    r++;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            cout << ans[i];
        }
        cout << endl;

    }

    return 0;
}
3
// 大数gcd
#include<iostream>
#include<string>
using namespace std;
int m(string& a, int b) {
    int i, l = a.size(), ans = 0;
    for (int i = 0; i < l; i++) {
        ans = ans * 10 + a[i] - '0';
        ans = ans % b;
    }
    return ans;
}

long long g(string  a, long long b) {
    if (m(a, b) == 0) return b;
    return g(to_string(b), m(a, b));
}

int main() {
    string a;
    long long b;
    cin >> a; cin >> b;
    long long ans = g(a, b);
    cout << m(a, b) << endl;
    cout << ans;
    return 0;
}
4
// 类似最大岛屿
#include<iostream>
#include<vector>
#include<string>
using namespace std;

void dfs(vector<string>& ve, vector<vector<bool>>& vis, int i, int j, int& tem) {
    if (i < 0 || i >= ve.size() || j < 0 || j >= ve[0].size() || ve[i][j] == '#' || vis[i][j]) return;
    vis[i][j] = true;
    tem++;
    dfs(ve, vis, i - 1, j, tem);
    dfs(ve, vis, i + 1, j, tem);
    dfs(ve, vis, i, j - 1, tem);
    dfs(ve, vis, i, j + 1, tem);
}
int main() {
    vector<string> ve;
    int n, m; cin >> n >> m;
    string str;
    int ans = 0;
    vector<vector<bool>> vis(n, vector<bool>(m, false));
    for (int i = 0; i < n; i++) {
        cin >> str;
        ve.push_back(str);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int tem = 0;
            if (ve[i][j] != '#' && vis[i][j] == false) {
                dfs(ve, vis, i, j, tem);
                ans = max(ans, tem);
            }
        }
    }
    cout << ans;
    return 0;
}




#寒武纪##笔试题目#
全部评论
全a么,我也做了,cv难民
2 回复
分享
发布于 2020-09-29 19:25
感觉题这么简单,怕是没有hc了吧🤣
点赞 回复
分享
发布于 2020-09-30 01:13
百信银行
校招火热招聘中
官网直投
第二题大佬可以讲下思路吗?我总是超时,只能过80%
点赞 回复
分享
发布于 2020-09-30 09:40

相关推荐

3 3 评论
分享
牛客网
牛客企业服务