深信服校园招聘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;
}

猜你喜欢

转载自blog.csdn.net/qq_40946921/article/details/106577217