腾讯2019秋招技术岗笔试题解(2018.9.16)

第1题:打表找规律

你会发现答案就是找1-n之间的最大的数满足:它的质因数只有一个数。如25,质因数只有5。12的质因数有2,3。结果 = 这个数 * 2。

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

typedef long long LL;
bool isPrime(LL n)
{
    set<int> s;
    for (int i = 2; i <= n; i++) {
        while (n % i == 0) {
            s.insert(i);
            n /= i;
        }
    }
    if (s.size() == 1) return true;
    return false;
}

const int maxn = 1e6 + 5;
LL n, tmp = 1, ans[maxn];

int main()
{
    bool flag = false;
    while (cin >> n) {
        for (int i = n; i > 0; i--) {
            if (isPrime(i)) {
                cout << i * 2 << endl;
                break;
            }
        }
    }
    return 0;
}

第2题:暴力宽度搜索BFS

首先去除重边和自环边,然后BFS找出入度和出度城市的个数。

#include 
using namespace std;
const int maxn = 1e3 + 5;
int main()
{
    int n, m;
    cin >> n >> m;
    vector > graph(maxn);
    for (int i = 0; i < m; i++) {
        int u, v ;
        cin >> u >> v;
        if(u == v) continue;
        graph[u].insert(v);
    }
    int out[maxn] = {0}, in[maxn] = {0};
    for(int i = 1; i <= n; i++) {
        queue q;
        set used = {i};
        q.push(i);
        while(!q.empty()) {
            int root = q.front();
            q.pop();
            for(int elem : graph[root]) {
                if(used.find(elem) != used.end()) continue;
                used.insert(elem);
                q.push(elem);
                in[elem]++, out[i]++;
            }
        }
    }
    int cnt = 0;
    for(int i = 1; i <= n; i++) {
        if(in[i] > out[i]) cnt++;
    }
    cout << cnt << endl;
    return 0;
}

第3题:数学题

选取的数都是A的倍数,所以加起来的和一定是A的倍数,因为A的值在0-100区间,直接暴力枚举100个A的倍数看是否存在A%B==C。

#include 
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int A, B, C;
        cin >> A >> B >> C;
        bool flag = false;
        for (int i = 1; i <= 100; i++) {
            int tmp = A * i;
            if (tmp % B == C) {
                flag = true;
                break;
            }
        }
        if (flag) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}
#腾讯##笔试题目##秋招##题解#
全部评论

相关推荐

评论
点赞
18
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务