开发笔试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届暑期实习正式批笔试##腾讯#
全部评论
两点距离那一题我也是暴力、、但不知道为什么你比我多了10分😂
1 回复
分享
发布于 2020-04-29 08:57
扑克牌那题思路是什么呀
1 回复
分享
发布于 2020-05-07 18:28
百信银行
校招火热招聘中
官网直投

相关推荐

B站 运营岗 普通Offer是12-15k*15-18,SP的Offer月薪16-17k*15,SSP的offer月薪是20k*15,综合年总包区间在18-30W。
点赞 评论 收藏
转发
6 6 评论
分享
牛客网
牛客企业服务