开发笔试460(100 60 100 100 100)代码
第一道题:模拟队列操作
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
int n;
cin >> n;
cin.ignore(20, '\n');
string line;
queue<int>temp;
for (int j = 0; j < n; j++)
{
getline(cin, line);
if (line.find("PUSH") != -1)
{
int num = stoi(line.substr(5, line.length()));
temp.push(num);
}
else if (line.find("TOP") != -1)
{
if (temp.empty())
{
cout << -1 << endl;
}
else
{
cout << temp.front() << endl;
}
}
else if (line.find("POP") != -1)
{
if (temp.empty())
{
cout << -1 << endl;
}
else
{
temp.pop();
}
}
else if (line.find("SIZE") != -1)
{
cout << temp.size() << endl;
}
else if (line.find("CLEAR") != -1)
{
while (!temp.empty())
{
temp.pop();
}
}
}
}
return 0;
}第二题:求两个集合最近的点
没学过ACM,只能暴力求解,求教这里有什么建议算法吗
#include<bits/stdc++.h>
using namespace std;
struct POINT
{
int x;
int y;
};
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
int n;
cin >> n;
vector<POINT>aPoints;
vector<POINT>bPoints;
for (int j = 0; j < n; j++)
{
POINT p;
cin >> p.x >> p.y;
aPoints.push_back(p);
}
for (int j = 0; j < n; j++)
{
POINT p;
cin >> p.x >> p.y;
bPoints.push_back(p);
}
double least = DBL_MAX;
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
double distance = pow((aPoints[j].x - bPoints[k].x), 2) + pow((aPoints[j].y - bPoints[k].y), 2);
if (distance < least)
{
least = distance;
}
if (least == 0)
{
break;
}
}
break;
}
cout.precision(3);
cout.setf(ios::fixed);
cout << sqrt(least) << endl;
}
return 0;
}第三题:扑克牌
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int>a;
vector<int>b;
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
a.push_back(temp);
}
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
b.push_back(temp);
}
int count = 0;
bool isChanged = false;
bool isRight = true;
while (true)
{
isChanged = false;
isRight = true;
for (int i = 0; i < n - 1; i++)
{
if (a[i] > a[i + 1])
{
isRight = false;
if (b[i + 1] <= b[i])
{
count++;
isChanged = true;
int temp = a[i];
a[i] = b[i + 1];
b[i + 1] = temp;
temp = a[i + 1];
a[i + 1] = b[i];
b[i] = temp;
}
}
}
if (!isRight && !isChanged)
{
cout << -1 << endl;
return 0;
}
else if (isRight)
{
break;
}
}
cout << count << endl;
return 0;
}第四题:队列模拟数据增强版
题目要求用两个栈模拟,我先尝试了一下STL库的队列也能AC。。。。
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int n;
cin >> n;
queue<long long int>temp;
string line;
cin.ignore(20, '\n');
for (int i = 0; i < n; i++)
{
getline(cin, line);
if (line.find("add") != -1)
{
long long int num = stoll(line.substr(4, line.length()));
temp.push(num);
}
else if (line.find("peek") != -1)
{
if (!temp.empty())
{
cout << temp.front() << endl;
}
}
else if (line.find("poll") != -1)
{
if (!temp.empty())
{
temp.pop();
}
}
}
return 0;
}第五题:求完全二叉树的祖先节点
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
long long int x;
int k;
cin >> x >> k;
int depth = (int)(log(x) / log(2)) + 1;
if (depth <= k)
{
cout << -1 << endl;
}
else
{
long long int temp = x;
while (depth != k)
{
temp = temp / 2;
depth--;
}
cout << temp << endl;
}
}
}#腾讯2021届暑期实习正式批笔试##腾讯#
美的集团公司福利 798人发布