腾讯4.26后台开发笔试(假算法)

除了第二题bao过60%,其他的都瞎搞过了。


第一题:

int main()
{
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        queue q;
        for (int i = 0; i < n; i++) {
            string str;
            cin >> str;
            if (str == "PUSH") {
                int t;
                cin >> t;
                q.push(t);
            } else if (str == "TOP") {
                if (q.empty()) {
                    cout << "-1" << endl;
                } else {
                    cout << q.front() << endl;
                }

            } else if (str == "POP") {
                if (q.empty()) {
                    cout << "-1" << endl;
                } else {
                    q.pop();
                }
            } else if (str == "SIZE") {
                cout << q.size() << endl;
            } else {
                while (!q.empty()) q.pop();
            }
        }
    }
    return 0;
}

第二题:
想破头也是暴力,我怎么想不出来

set a, b;

double getDis(double x1, double y1, double x2, double y2) {
    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}

int main()
{
    int T;
    cin >> T;
    while (T--) {
        int n, flag = 1;
        a.clear(), b.clear();
        cin >> n;
        for (int i = 0; i < n; i++) {
            double x, y;
            cin >> x >> y;
            pll p = make_pair(x, y);
            if (!a.count(p)) {
                a.insert(p);
            }
        }
        for (int i = 0; i < n; i++) {
            double x, y;
            cin >> x >> y;
            pll p = make_pair(x, y);
            if (!b.count(p)) {
                b.insert(p);
            }
            if (a.count(p)) {
                flag = 0;
            }
        }
        if (flag) {
            double ans = 0x3f3f3f3f;
            for (auto p1 : a) {
                double x1 = p1.first, y1 = p1.second;
                double pre = 0x3f3f3f3f;
                for (auto p2 : b) {
                    double x2 = p2.first, y2 = p2.second;
                    double dis = getDis(x1, y1, x2, y2);
                    ans = min(ans, dis);
                    if (pre < dis) {
                        break;
                    }
                    pre = dis;
                }
            }
            printf("%.3f\n", ans);
        } else {
            cout << "0.000" << endl;
        }
    }

    return 0;
}

第三题:
瞎搞过了之后,一开始觉得有点开心,但是总觉得这个不是正解。笔试完发现是状压DP,唉,就觉得有点难过,我到底学得啥呀……

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", &b[i]);
    }
    while (1) {
        int t = ans;
        for (int i = 0; i < n - 1; i++) {
            if (a[i] > a[i + 1]) {
                swap(a[i], b[i]);
                swap(a[i + 1], b[i + 1]);
                swap(a[i], a[i + 1]);
                swap(b[i], b[i + 1]);
                ans++;
            }
        }
        if (t == ans) {
            break;
        }
    }

    cout << ans << endl;
    return 0;
}

第四题:

int main()
{
    queue q;
    int n, x;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string str;
        cin >> str;
        if (str == "add") {
            cin >> x;
            q.push(x);
        } else if (str == "poll") {
            q.pop();
        } else {
            cout << q.front() << endl;
        }
    }
    return 0;
}

第五题:
别问我为什么用Java,问就是想起了HashMap的源码……

public class Main {

    static int getLevel(long x) {
        long n = x;
        int level = 0;
        while (n > 0) {
            n >>>= 1;
            level++;
        }
        return level;
    }
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n;
        n = cin.nextInt();
        for (int i = 0; i < n; i++) {
            long x = cin.nextLong();
            int k = cin.nextInt();
            int level = getLevel(x);
            if (level <= k) {
                System.out.println(-1);
            } else {
                for (int j = 0; j < level - k; j++) {
                    x >>>= 1;
                }
                System.out.println(x);
            }
        }

        cin.close();
    }
}

假算法虽然有可能帮你过笔试,但是得到的只是无比短暂的快乐,只有掌握了真正的算法,才能让内心感到充实。唉,蒟蒻的快乐就是那么朴实无华,且枯燥……

挂一波AK大佬的链接:4.26号腾讯笔试题(AK)

祝各位看官 Offer++;

#腾讯笔试##腾讯##笔试题目#
全部评论
第二题二分判断呀,只要找出一个点然后在这个半径范围内的xy判断下更新就好了~
点赞 回复 分享
发布于 2020-04-27 01:18
第四题真搞心态,做的时候还特意去想用栈怎么做,还担心做***超时
点赞 回复 分享
发布于 2020-04-27 00:29
第一题好像不能AC吧。。。都是这么写的都是AC 0
点赞 回复 分享
发布于 2020-04-27 00:06

相关推荐

不愿透露姓名的神秘牛友
07-24 13:39
在记录秋招的大魔王很...:别被忽悠了,我做了多年销售。我可以告诉你,这就是忽悠你的,销售一定要看底薪也要看提成两者不可缺一。提成是有业绩的时候才拿的到的,谁能保证一直有单状态都好。销售有时候很讲究运气的。底薪是你这个人这个岗位日常工作体现的价值。别小看底薪,你看那些跳槽去做经理主管的,底薪底一些,人家愿意去吗?所以那些说销售靠提成的纯属忽悠,除非他们的业务很容易成单。
点赞 评论 收藏
分享
07-03 16:13
嘉应学院 Python
xiaolihuam...:很明显骗子,如果是hr直接约你面试了,哪用得着内推,如果是员工的话,你得多优秀,一线员工直接加你微信,
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-23 14:13
这是聊岔撇了吗,相同的话问了两遍
吴offer选手:上下文切换这一块
点赞 评论 收藏
分享
评论
4
10
分享

创作者周榜

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