首页 > 网易互娱笔试3个编程题
头像
晓麦
编辑于 2020-04-11 23:11
+ 关注

网易互娱笔试3个编程题

1、四十米大刀
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

bool compare(pair<int, int>& p1, pair<int, int>& p2)
{
    if(p1.first == p2.first)
    {
        return p1.second < p2.second;
    }
    return p1.first < p2.first;
}

void getlength(vector<vector<int>>& vec, int m, int& l, int x, int y)
{
    vector<pair<int, int>> res(m*m);
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<m; j++)
        {
            int key = (i-x)*(i-x)+(j-y)*(j-y);
            res[i*m+j].first = key;
            res[i*m+j].second = vec[i][j];
        }
    }
    sort(res.begin(), res.end(), compare);
    for(int i=0; i<res.size(); i++)
    {
        if(l*l >= res[i].first)
        {
            l += res[i].second;
        }
        else
        {
            break;
        }
    }
}

int main()
{
    int t;
    while(cin>>t)
    {
        while(t)
        {
            int m, l;
            cin>>m>>l;
            vector<vector<int> > vec(m, vector<int>(m));
            for(int i=0; i<m; i++)
            {
                for(int j=0; j<m; j++)
                {
                    cin>>vec[i][j];
                }
            }
            int x, y;
            cin>>x>>y;
            getlength(vec, m, l, x, y);
            cout<<l<<endl;
            t--;
        }
        
    }
}



2、对1~N一共N个数,初始时各自有一个集合,对这些集合可以采取如下几种操作:
- (1)如果x,y不在一个集合里,则将x和y所在的集合合并
- (2)如果x所在集合中除了x还有别的数字,则将x提出单独成立一个集合
- (3)输出x所在集合的元素个数
#include <iostream>
#include <vector>
#include <map>
using namespace std;

struct node
{
    int value;
    node* pre;
    node* next;
    node(int v):value(v), pre(nullptr), next(nullptr){};
};


int main()
{
    int t;
    while(cin>>t)
    {
        while(t)
        {
        int n, m;
        cin>>n>>m;
        map<int, node*> mymap;
        for(int i=1; i<=n; i++)
        {
            node* tmp = new node(i);
            tmp->pre = tmp;
            tmp->next = tmp;
            mymap[i] = tmp;
        }
        
        int op, x, y;
        while(m)
        {
            cin>>op;
            switch(op)
            {
                case 1:
                {
                    cin>>x>>y;
                    node* temp = mymap[x]->next;
                    while(temp != mymap[x])
                    {
                        if(temp->value == mymap[y]->value)
                        {
                            break;
                        }
                        temp = temp->next;
                    }
                    if(temp == mymap[x])
                    {
                        node* prex = mymap[x]->pre;
                        node* prey = mymap[y]->pre;
                        prex->next = mymap[y];
                        mymap[y]->pre = prex;
                        prey->next = mymap[x];
                        mymap[x]->pre = prey;
                    }
                    
                    break;
                }
                case 2:
                {
                    cin>>x;
                    if(mymap[x]->next != mymap[x])
                    {
                        node* temp = mymap[x]->pre;
                        temp->next = mymap[x]->next;
                        mymap[x]->next->pre = temp;
                        mymap[x]->pre = mymap[x];
                        mymap[x]->next = mymap[x];
                    }
                    break;
                }
                case 3:
                {
                    cin>>x;
                    node* temp = mymap[x]->next;
                    int res = 1;
                    while(temp != mymap[x])
                    {
                        res++;
                        temp = temp->next;
                    }
                    cout<<res<<endl;
                    break;
                }
                default:
                    break;
            }
            m--;
        }
        t--;
    }
    }
    return 0;
}



3、最小错排
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int t;
    while(t)
    {
        int n;
        cin>>n;
        vector<int> vec1(n);
        vector<int> vec2(n);
        for(int i=0; i<n; i++)
        {
            cin>>vec1[i];
        }
        int min = 100;
        int res = 0;
        for(int i=0; i<n; i++)
        {
            cin>>vec2[i];
            if(i%2 == 0 && vec2[i] < min)
            {
                min = vec2[i];
            }
            res += vec2[i];
        }
        if(n%2 == 1)
        {
            res += min;
        }
        cout<<res<<endl;
        t--;
    }

    return 0;
}


全部评论

(2) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

热门推荐