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);
}
}
}
} #腾讯笔试##笔试题目##腾讯#
查看8道真题和解析
