9.27网易互娱笔试题1,2题代码

第一题

关于蛇形遍历数组的题目,AC
代码如下:

#include<bits/stdc++.h>

using namespace std;

vector<vector<int>> prepare(int n)
{
    vector<vector<int>> data(n, vector<int>(n, 0));

    int left = 0, right = n-1;
    int up = 0, down = n-1;

    int cnt = 1;
    int flag = 1;
    while(cnt <= n*n)
    {
        if(flag % 2 == 1)
        {

            for(int k = left; k <= right; k++) {
                data[up][k] = cnt++;
            }
            up++;

            for(int k = up; k <= down; k++)
            {
                data[k][right] = cnt++;
            }
            right--;

            for(int k = right; k >= left; k--)
            {
                data[down][k] = cnt++;
            }
            down--;

            for(int k = down; k >= up; k--)
            {
                data[k][left] = cnt++;
            }
            left++;



            flag ++;
        }else
        {
            for(int k = up; k <= down; k++)
            {
                data[k][left] = cnt++;
            }
            left++;

            for(int k = left; k <= right; k++)
            {
                data[down][k] = cnt++;
            }
            down--;

            for(int k = down; k >= up; k--)
            {
                data[k][right] = cnt++;
            }
            right--;

            for(int k= right; k >= left; k--)
            {
                data[up][k] = cnt++;
            }
            up++;


            flag++;
        }
    }

    return data;
}

int main()
{
    int C;
    cin >> C;

    vector<vector<int>> res;
    for(int i = 0; i < C; i++)
    {
        int n, m;
        cin >> n >> m;
        res = prepare(n);

        for(int j = 0; j < m; j++)
        {
            int a, b;
            cin >> a >> b;
            cout << res[a][b];

            if(j < m-1) cout <<endl;
        }

        if(i < C-1) cout << endl;
    }

    return 0;
}

第二题

模拟文件系统的题目,A50%,感觉对于如何选取当前最小的数字当做文件描述符,没有找到时间复杂度低的方法,这里使用的是暴力枚举

测试样例:
1
10
open libc.so
open libm.so
open libdl.so
dup 2
dup2 0 2
close 0
query 1
query 2
query 3
open log.txt

输出:
0
1
2
3
open libm.so
open libc.so
open libdl.so
0

#include<bits/stdc++.h>

using namespace std;

vector<int> mask(1000010, 0);
//int curmin = 0;
unordered_map<int, string> ump;


int main()
{
    int T;
    cin >> T;
    for(int i = 0; i < T; i++)
    {
        mask = vector<int>(1000010, 0);
        ump = unordered_map<int, string>();
        //curmin = 0;

        int num;
        cin >> num;
        getchar();

        for(int j = 0; j < num; j++)
        {
            string str;
            getline(cin, str);
            //cout << str << endl;

            if(str[0] == 'o')
            {
                string temp = str.substr(5, str.size()-5);
                int flag;
                for(flag = 0; flag < 1000000; flag++) {
                    if (mask[flag] == 0) {
                        break;
                    }
                }

                cout << flag << endl;
                mask[flag] = 1;
                ump[flag] = temp;

            }else if(str[0] == 'c')
            {
                string temp = str.substr(6, str.size() - 6);
                int tempint = stoi(temp);

                mask[tempint] = 0;
                ump.erase(tempint);


            }else if(str[0] == 'q')
            {
                string temp = str.substr(6, str.size() - 6);
                int tempint = stoi(temp);
                cout << ump[tempint] << endl;

            }else if(str[0] == 'd')
            {
                if(str[3] == '2')
                {
                    string temp = "";
                    int k = 5;
                    for(;k < str.size(); k++)
                    {
                        if(str[k] != ' ')
                        {
                            temp+= str[k];
                        }else
                        {
                            break;
                        }
                    }
                    int left = stoi(temp);

                    temp = "";
                    for(;k < str.size(); k++)
                    {

                        temp+= str[k];
                    }

                    int right = stoi(temp);
                    mask[right] = 1;


                    ump[right] = ump[left];


                }else
                {
                    string temp = str.substr(4, str.size() - 4);
                    int tempint = stoi(temp);
                    string file = ump[tempint];

                    int flag;
                    for(flag = 0; flag < 1000000; flag++) {
                        if (mask[flag] == 0) {
                            break;
                        }
                    }

                    cout << flag << endl;
                    mask[flag] = 1;
                    ump[flag] = file;

                }
            }

        }
    }

    return 0;
}
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页