深信服校園招聘c/c++軟件開發A卷

題目鏈接:https://www.nowcoder.com/test/23090658/summary

1、圍棋遍歷

函數calc計算圍棋中位置(x,y)處連成一片的棋子個數。所謂連成一片,即沿着棋盤橫豎線往任意方向遍歷,遍歷過程允許轉彎,不允許走斜線,中間未出現對方棋子或空子。

enum color {

    NONE, WHITE, BLACK,         // 棋子顏色,NONE表示未落子

};

struct weiqi {

    enum color board[19][19];   // 棋盤上每個位置的落子

};

int calc(struct weiqi *wq, int x, int y){}

輸入描述:

第1-19行數據是棋盤上棋子的顏色數據。0表示未落子,1表示白子,2表示黑子。 第1行最左邊位置的座標是(0,0),第1行第2列的座標是(1,0),第2行第1列的座標是(0,1),依此類推。 第20行數據是起始座標(x,y)

輸出描述:

與座標(X,Y)連成一片的棋子數目

輸入例子1:

0000000000000000000
0000011000000000000
0000001111000000000
0000001021000000000
0000001010100000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
5,1

輸出例子1:

9
#include <stdio.h>
#include <string.h>
 
enum color {
    NONE, WHITE, BLACK,         //棋子顏色,NONE表示未落子
};
struct weiqi {
    enum color board[19][19];   //棋盤上每個位置的落子
};
 
int calc(struct weiqi* wq, int x, int y)
{
    static int dir[][2] = { {1,0},{0,1},{0,-1},{-1,0} };
    static color current = wq->board[y][x];
    if (x >= 19
        || x < 0
        || y >= 19
        || y < 0
        || wq->board[y][x] != current
        || wq->board[y][x] == NONE
        )
        return 0;
    int counter = 1;
    wq->board[y][x] = NONE;
    for (int i = 0; i< 4; i++)
        counter+=calc(wq, x + dir[i][0], y + dir[i][1]);
    return counter;
}
 
int input(struct weiqi* wq, int* x, int* y)
{
    int row, col;
    int ret;
    char buf[80];
 
    for (row = 0; row < 19; ++row) {
        if (fgets(buf, sizeof(buf), stdin) == NULL)
            return -1;
        if (strlen(buf) < 19)
            return -1;
        for (col = 0; col < 19; ++col) {
            switch (buf[col]) {
            case '0':
                wq->board[row][col] = NONE;
                break;
            case '1':
                wq->board[row][col] = WHITE;
                break;
            case '2':
                wq->board[row][col] = BLACK;
                break;
            default:
                return -1;
            }
        }
    }
    ret = scanf( "%d,%d", x, y);
    if (ret != 2)
        return -1;
    for (row = 0; row < 19; ++row) {
        for (col = 0; col < 19; ++col) {
            fprintf(stderr, "%d ", wq->board[row][col]);
        }
        fprintf(stderr, "\n");
    }
    fprintf(stderr, "x = %d, y = %d\n", *x, *y);
    return 0;
}
 
int main()
{
    struct weiqi wq;
    int x = 0, y = 0;
    int cnt;
 
    memset(&wq, 0, sizeof(wq));
    if (input(&wq, &x, &y) < 0) {
        fprintf(stderr, "error!\n");
        return 1;
    }
    cnt = calc(&wq, x, y);
 
    printf("%d\n", cnt);
    return 0;
}

2、單鏈表排序

請實現list_sort,使用冒泡法將head指向的鏈表按val值大小排成升序

struct node {

    int val;

    struct node *next;

};

void list_sort(struct node *head)

{

}

static void list_sort(struct node* head)
{
    for (node* i = head; i; i=i->next) {
        for (node* j = i->next; j; j = j->next) {
            if (i->val > j->val)
                std::swap(i->val, j->val);
        }
    }
}

3、出棧順序

已知某一個字母序列,把序列中的字母按出現順序壓入一個棧,在入棧的任意過程中,允許棧中的字母出棧,求所有可能的出棧順序

輸入描述:

字符串,如:abc

輸出描述:

可能的出棧順序,每行一種順序

輸入例子1:

abc

輸出例子1:

abc
acb
bac
bca
cba
#include <iostream>
#include <string>
#include <vector>
#include <set>
using namespace std;
int main() {
    string str;
    cin >> str;
    vector<pair<string, string>> result;    //(棧中字符,出棧字符)
    result.push_back({ string(),string() });
    for (const char& ch : str) {
        int m_size = result.size();
        for (int i = 0; i < m_size; i++) {
            result[i].first.push_back(ch); 
            pair<string, string> temp = result[i];
            for (int j = 0; j < result[i].first.size(); j++) {
                temp.second.push_back(temp.first.back());
                temp.first.pop_back();
                result.push_back(temp);
            }
        }
    }
    set<string> output;
    for (auto& stk : result)
        output.insert(stk.second + string(stk.first.rbegin(), stk.first.rend()));
    for (const string& str : output)
        cout << str << endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章