腾讯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++;
#腾讯笔试##腾讯##笔试题目#
海康威视公司福利 1125人发布