4.26腾讯笔试后台开发5道编程题

  • 总体来说,好像只有第2题较难?我直接暴力得50%,还有第4题,用了2个栈模拟只得了80%,交了n次还是一样。。。第三题数据太水,假AC过去了。。

  • 先说说我的战绩吧,大神别喷!
    A:100%,B:50%,C:100%,D:80%,E:100%
    本人只AC了430%,留下了菜鸡的泪水。。。有全A的大佬麻烦评论区说一下思路,多谢拉!

  • 顺便许个愿希望腾讯爸爸给小弟一个面试机会吧,

  • 另外,做题半个小时之后,有北京的电话打给我,我直接挂了,不知道是哪家公司打电话给我,凉凉,错失了一个机会,呜呜呜...我打过去是座机号:010567*****,有和我一样情况的麻烦告诉我一下该怎么办,多谢各位大佬!

  • 接下来贴一下代码:

  • A:100%

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
// t1
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = Integer.parseInt(in.nextLine());
        int Q, len, x;
        String str;
        String[] arr;
        Queue<Integer> queue;
        while (T-- > 0) {
            Q = Integer.parseInt(in.nextLine());
            queue = new ArrayDeque<Integer>();
            while (Q-- > 0) {
                str = in.nextLine();
                arr = str.split(" ");
                len = arr.length;
                if (len == 1) {
                    if (arr[0].equals("TOP"))
                        System.out.println(queue.isEmpty() ? -1 : queue.peek());
                    else if (arr[0].equals("POP")) {
                        if (queue.isEmpty())
                            System.out.println(-1);
                        else
                            queue.remove();
                    } else if (arr[0].equals("SIZE"))
                        System.out.println(queue.size());
                    else
                        queue.clear();
                } else {
                    queue.add(Integer.parseInt(arr[1]));
                }
            }
        }
    }
}
  • B:50%,直接暴力。。60%应该是考虑了数据溢出。
import java.util.Scanner;
// t2
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while (T-- > 0) {
            int n = in.nextInt();
            Node[] A = new Node[n];
            Node[] B = new Node[n];
            for (int i = 0; i < n; i++) {
                A[i] = new Node();
                A[i].x = in.nextInt();
                A[i].y = in.nextInt();
            }
            for (int i = 0; i < n; i++) {
                B[i] = new Node();
                B[i].x = in.nextInt();
                B[i].y = in.nextInt();
            }
            double ans = Double.MAX_VALUE;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    ans = Math.min(ans, dist(A[i], B[j]));
            System.out.println(String.format("%.3f", ans));
        }
    }
    // 求两点之间的距离
    static double dist(Node a, Node b) {
        return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }
}
class Node {
    int x, y;
}
  • C:100%,暴力搜就完事了。好像没考虑无解的情况?当时想骗点分,没想到提交一发直接就AC了就不多想了....
import java.util.Scanner;
// t3
public class Main {
    static boolean isOK;
    static Node[] nodes;
    static int len;
    static int ans;
    static boolean[] vis;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 262144
        int n = in.nextInt();
        nodes = new Node[n];
        len = n;
        vis = new boolean[n];
        for (int i = 0; i < n; i++)
            nodes[i] = new Node();
        for (int i = 0; i < n; i++)
            nodes[i].a = in.nextInt();
        for (int i = 0; i < n; i++)
            nodes[i].b = in.nextInt();
        isOK = false;
        ans = Integer.MAX_VALUE;
        dfs(0);
        System.out.println(ans);

    }
    static boolean check() {
        boolean flag = true;
        for (int i = 1; i < len; i++) {
            if (nodes[i - 1].a > nodes[i].a) {
                flag = false;
                break;
            }
        }
        return flag;
    }
    static void dfs(int cnt) {
        if (check()) {
            ans = Math.min(ans, cnt);
            return;
        }
        for (int i = 1; i < len; i++) {
            if (nodes[i - 1].a > nodes[i].a) {
                swap(i - 1, i);
                dfs(cnt + 1);
            }
        }
    }
    static void swap(int i, int j) {
        int tmp = nodes[i].a;
        nodes[i].a = nodes[i].b;
        nodes[i].b = tmp;
        tmp = nodes[j].a;
        nodes[j].a = nodes[j].b;
        nodes[j].b = tmp;
        tmp = nodes[i].a;
        nodes[i].a = nodes[j].a;
        nodes[j].a = tmp;
        tmp = nodes[i].b;
        nodes[i].b = nodes[j].b;
        nodes[j].b = tmp;
    }
}
class Node {
    int a, b;
}
  • D: 80%,力扣原题。。。看了用java写的这题都没AC100%?不知道是不是卡java时间复杂度?当时想改用C++写,发现一直卡在输入输出,流下了遗忘东西的泪水。。。
import java.util.Scanner;
import java.util.Stack;
// t4
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = Integer.parseInt(in.nextLine());
        String str;
        String[] arr;
        int len;
        Stack<Integer> stack1 = new Stack<Integer>(), stack2 = new Stack<Integer>();
        while (N-- > 0) {
            str = in.nextLine();
            arr = str.split(" ");
            len = arr.length;
            if (len == 1) {
                if (stack2.isEmpty()) {
                    while (!stack1.isEmpty())
                        stack2.push(stack1.pop());
                }
                if (arr[0].equals("peek"))
                    System.out.println(stack2.peek());
                else
                    stack2.pop();
            } else
                stack1.push(Integer.parseInt(arr[1]));
        }
    }
}
  • E:100%,简单数学。
import java.util.Scanner;
// t5
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int Q = in.nextInt();
        long x, y;
        int k, height;
        while (Q-- > 0) {
            x = in.nextLong();
            k = in.nextInt();
            height = 0;
            y = x;
            while (x > 0) {
                x /= 2;
                height++;
            }
            if (k >= height)
                System.out.println(-1);
            else {
                while (height > k) {
                    y /= 2;
                    height--;
                }
                System.out.println(y);
            }
        }
    }
}
#腾讯笔试##笔试题目##腾讯#
全部评论
只AC了430%。。。。
6 回复
分享
发布于 2020-04-26 22:13
老铁,已经是巨佬了,我只AC第一题😂😂
2 回复
分享
发布于 2020-04-26 22:16
OPPO
校招火热招聘中
官网直投
然后这么巧的吗 我也是一个北京的电话 直接挂了,,,写的骚扰电话。。。
1 回复
分享
发布于 2020-04-26 22:28
第四题时间来不及用第一题的模板改的,,会被查么。。
1 回复
分享
发布于 2020-04-26 22:30
太强了
1 回复
分享
发布于 2020-04-26 22:36
阿里的吧
点赞 回复
分享
发布于 2020-04-26 22:14
我双栈也只用80%,第二题也是只有暴力的50%,太难了
点赞 回复
分享
发布于 2020-04-26 22:14
只ac了3.1&nbsp;&nbsp;第三题茫然没下手&nbsp;第四题无论怎么优化也只有85%&nbsp;leetcode做了n次的原题了
点赞 回复
分享
发布于 2020-04-26 22:15
第二题python暴力是0
点赞 回复
分享
发布于 2020-04-26 22:15
请问一下为什么我做的题目跟你们的完全不一样?😅😅
点赞 回复
分享
发布于 2020-04-26 22:17
上面写的是数据分析岗位,我在牛客上没看到跟我一样题目的,难道给我发错了卷子?😭
点赞 回复
分享
发布于 2020-04-26 22:18
请问这样为啥过不了。。。 #include<iostream> #include<algorithm> using namespace std; int n; int a[20]; int b[20]; int dp[20]; int cnt; int ans=0x3f3f3f3f; bool check(){ for(int i=2;i<=n;++i){ if(a[i]<a[i-1]) return false; } return true; } void dfs(int x,int cnt){ if(check()){ ans=min(ans,cnt); } if(cnt>ans){ return ; } for(int i=1;i<=n;++i){ swap(a[i-1],b[i-1]); swap(a[i],b[i]); swap(a[i],a[i-1]); swap(b[i],b[i-1]); dfs(x+1,cnt+1); } } int main(){ cin>>n; for(int i=1;i<=n;++i) cin>>a[i]; for(int j=1;j<=n;++j) cin>>b[j]; dfs(2,0); cout<<ans; return 0; }
点赞 回复
分享
发布于 2020-04-26 22:23
我接了一个是招行的电话
点赞 回复
分享
发布于 2020-04-26 22:30
大佬,麻烦帮我看下第一题代码,本地能过,但是 copy 过去死活 0%,不知道是哪里的问题。。。 import java.util.*; public class Main { Queue<Integer> help = new LinkedList<Integer>(); public static void main(String[] args) { Scanner in = new Scanner(System.in);         int n = in.nextInt(); Main test = new Main(); for(int i = 0; i < n; i++) { int k = in.nextInt(); for(int j = 0; j < k; j++) { String aStrings = in.next(); if(aStrings.equals("PUSH")) { test.help.add(in.nextInt()); } else if(aStrings.equals("TOP")) { if(test.help.isEmpty()) System.out.println(-1); else { System.out.println(test.help.peek()); } } else if(aStrings.equals("POP")) { if(test.help.isEmpty()) System.out.println(-1); else { System.out.println(test.help.poll()); } } else if(aStrings.equals("SIZE")) { System.out.println(test.help.size()); } else if(aStrings.equals("CLEAR")) { test.help.clear(); } } test.help.clear(); } } }
点赞 回复
分享
发布于 2020-04-26 22:55
大佬第三题和第五题的题目还记得吗?感谢😅
点赞 回复
分享
发布于 2020-04-26 23:36
谁来说说第二题的思路,除了暴力,还有啥办法
点赞 回复
分享
发布于 2020-04-26 23:56
哎呀我裂开,这个第三题,我用的广搜,但是我有个条件没看到,只能交换相邻的卡片,我写的是任意位置交换的广搜代码,难怪我只能通过20%,透了,相邻的那就简单多了,靠!
点赞 回复
分享
发布于 2020-04-27 03:21
想问一下,第三题有考虑无解的情况吗
点赞 回复
分享
发布于 2020-04-27 04:25
我也是八点半接了美团的🤣
点赞 回复
分享
发布于 2020-04-27 07:01
我问一下 你们是大学生 还是工作几年的人了  怎么都这么厉害
点赞 回复
分享
发布于 2020-04-27 08:08

相关推荐

10 65 评论
分享
牛客网
牛客企业服务