9.27網易互娛筆試題1,2題程式碼

2020-09-30 13:00:33

第一題

關於蛇形遍歷陣列的題目,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;
}