西农数据结构第二次实习题目参考

T1

用顺序存储实现栈的初始化、入栈、出栈、取栈顶、判栈空操作。调用以上操作实现判断从键盘输入的括号序列是否匹配

输入描述

括导序列#(#为括号输入结束符号)

输出描述

匹配或不匹配

输入

{[()]}#

输出

匹配

#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
class Stack {
private:
    char data[MAX_SIZE];
    int top;
public:
    Stack() {
        top = -1;
    }
    void init() {
        top = -1;
    }
    bool push(char item) {
        if (top == MAX_SIZE - 1) {
            return false;
        }
        data[++top] = item;
        return true;
    }
    char pop() {
        if (top == -1) {
            return '\0';
        }
        return data[top--];
    }
    char peek() {
        if (top == -1) {
            return '\0';
        }
        return data[top];
    }
    bool isEmpty() {
        return top == -1;
    }
};
bool isMatchingBrackets(string input) {
    Stack stack;
    stack.init();
    for (char c : input) {
        if (c == '(' || c == '[' || c == '{') {
            stack.push(c);
        }
        else if (c == ')' || c == ']' || c == '}') {
            if (stack.isEmpty()) {
                return false;
            }
            char top = stack.pop();
            if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) {
                return false;
            }
        }
        else if (c == '#') {
            break;
        }
    }
    return stack.isEmpty();
}
int main() {
    string input;
    cin >> input;
    if (isMatchingBrackets(input)) {
        cout << "匹配" << endl;
    }
    else {
        cout << "不匹配" << endl;
    }
    return 0;
}

T2

患者到医院看病的顺序是,先排队等候,再看病治疗。要求设计个算法,模拟病人等候就诊的过程。其中:“病人到达"用命令“A”(或"a")表示,"护士让下一位就诊”用"N”(或"n")表示,“不再接收病人排队"用“S”(或“s”)表示,

输入描述

A(或a)病历号

N(或n)

S(或s)

X(其他字符)

输出描述

病历号为x的病人就诊

若队列中有数据,输出队首元素,否则输出“无病人就诊”

不再接收病人排队,并输出当前队列中所有病历号

输入命令不合法!

输入

A

123

n

n

a

124

A

125

X

S

输出

病历号为123的病人就诊

无病人就诊

输入命令不合法!

今天不再接收病人排队,下列排队的病人依次就诊:124 125

#include <iostream>
#include <cstring>
using namespace std;
const int MAX_SIZE = 100;
class PatientQueue {
private:
    int data[MAX_SIZE];
    int front;
    int rear;
public:
    PatientQueue() {
        front = rear = -1;
    }
    bool isEmpty() {
        return front == -1;
    }
    bool isFull() {
        return (rear + 1) % MAX_SIZE == front;
    }
    void enqueue(int patientNumber) {
        if (isFull()) {
            cout << "队列已满,无法加入新病人。" << endl;
            return;
        }
        if (isEmpty()) {
            front = rear = 0;
        }
        else {
            rear = (rear + 1) % MAX_SIZE;
        }
        data[rear] = patientNumber;
    }
    int dequeue() {
        if (isEmpty()) {
            cout << "无病人就诊" << endl;
            return -1;
        }
        int patientNumber = data[front];
        if (front == rear) {
            front = rear = -1;
        }
        else {
            front = (front + 1) % MAX_SIZE;
        }
        return patientNumber;
    }
    void printQueue() {
        if (isEmpty()) {
            cout << "当前队列中无病人。" << endl;
            return;
        }
        cout << "今天不再接收病人排队,下列排队的病人依次就诊:";
        int index = front;
        while (index != rear) {
            cout << data[index] << " ";
            index = (index + 1) % MAX_SIZE;
        }
        cout << data[index] << endl;
    }
};
int main() {
    PatientQueue queue;
    char command;
    int patientNumber;
    while (true) {
        cin >> command;
        if (command == 'A' || command == 'a') {
            cin >> patientNumber;
            queue.enqueue(patientNumber);
        }
        else if (command == 'N' || command == 'n') {
            int patient = queue.dequeue();
            if (patient != -1) {
                cout << "病历号为" << patient << "的病人就诊" << endl;
            }
        }
        else if (command == 'S' || command == 's') {
            queue.printQueue();
            break;
        }
        else {
            cout << "输入命令不合法!" << endl;
        }
    }
    return 0;
}

T3

迷宫是一个二维矩阵,其中1为墙,0为路,3为入口,4为出口.要求从入口开始,从出口结束,按照 下,左,上,右 的顺序来搜索路径

输入描述

迷宫宽度W 迷宫高度h

迷宫第一行

迷宫第二行

迷宫第n行

输出描述

入口横坐标1 入口纵坐标1

横坐标2 纵坐标2

横坐标3 纵坐标3

横坐标4 纵坐标4

横坐标n-1 纵坐标n-1

出口横坐标n 出口纵坐标n

输入

8 10

1 1 1 1 1 1 1 1

1 0 1 1 0 1 0 1

1 0 1 0 0 1 0 1

1 1 0 3 1 0 1 1

1 0 0 1 0 0 4 1

1 0 0 0 0 1 1 1

1 0 1 0 0 1 0 1

1 0 1 0 0 0 1 1

1 1 1 1 0 0 0 1

1 1 1 1 1 1 1 1

输出

3 3

2 3

2 4

2 5

3 5

3 6

3 7

4 7

4 6

4 5

4 4

5 4

6 4

#include <iostream>
#include <list>
using namespace std;

struct PosInfo {
    int px, py;
    PosInfo(int x, int y) { px = x; py = y; }
};

class MG {
    char** S;
    int w, h;
    int in_x, in_y, out_x, out_y;
    list<PosInfo> s;

public:
    MG(int w, int h) {
        this->w = w;
        this->h = h;
        this->S = new char* [h];
        for (int i = 0; i < h; i++) {
            this->S[i] = new char[w];
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> S[i][j];
                if (S[i][j] == '3') {
                    in_y = j;
                    in_x = i;
                }
                if (S[i][j] == '4') {
                    out_x = i;
                    out_y = j;
                }
            }
        }
        seekPath(in_x, in_y); 
    }

    bool could(int x, int y)
    {
        return (S[x][y] != '*' && S[x][y] != '1');
    }

    bool seekPath(int x, int y) {

        s.push_front(PosInfo(y, x));
        if (x == out_x && y == out_y) {
            if (s.empty()) { cout << "列表为空" << endl; return false; }
            while (!s.empty()) {
                cout << s.back().px << " " << s.back().py << endl;
                s.pop_back();
            }
            return true;
        }
        if (could(x, y)) {
            S[x][y] = '1';
            if (seekPath(x + 1, y)) return true;
            if (seekPath(x, y - 1)) return true;
            if (seekPath(x - 1, y)) return true;
            if (seekPath(x, y + 1)) return true;
        }

        s.pop_front();
        return false;
    }

    ~MG() {
        for (int i = 0; i < h; i++) {
            delete[] S[i];
        }
        delete[] S;
    }
};

int main() {
    int w, h;
    cin >> w >> h;
    MG m1(w, h);
    return 0;
}

T4

设计一个算法找一条从迷宫入口到出口的最短路径

输入描述

迷宫的行和列m n

迷宫的布局

输出描述

最小路径

输入

6 8

0 1 1 1 0 1 1 1

1 0 1 0 1 0 1 0

0 1 0 0 1 1 1 1

0 1 1 1 0 0 1 1

1 0 0 1 1 0 0 0

0 1 1 0 0 1 1 0

输出

(6,8)

(5,7)

(4,6)

(4,5)

(3,4)

(3,3)

(2,2)

(1,1)

#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;

struct Point {
    int x, y;
    Point(int x, int y) : x(x), y(y) {}
};

class MG {
    int m, n;
    int** S;
    vector<Point> path; 
    vector<vector<bool>> visited; 

public:
    MG(int m, int n) {
        this->m = m;
        this->n = n;
        S = new int* [m];
        for (int i = 0; i < m; i++) {
            S[i] = new int[n];
        }


        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                cin >> S[i][j];
            }
        }

        visited.resize(m, vector<bool>(n, false));
        searchPath();
        printPath();
    }
    bool could(int x, int y) {
        return (x >= 0 && x < m && y >= 0 && y < n && S[x][y] == 0 && !visited[x][y]);
    }
    void searchPath() {
        queue<pair<Point, vector<Point>>> q;
        q.push({ Point(0, 0), {Point(0, 0)} });
        visited[0][0] = true;

        vector<pair<int, int>> directions = {
            {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, -1}, {1, 1}, {-1, -1}, {-1, 1} 
        };

        while (!q.empty()) {
            auto current = q.front();
            q.pop();
            Point point = current.first;
            vector<Point> currentPath = current.second;
            if (point.x == m - 1 && point.y == n - 1) {
                path = currentPath; 
                return;
            }
            for (auto dir : directions) {
                int newX = point.x + dir.first;
                int newY = point.y + dir.second;

                if (could(newX, newY)) {
                    visited[newX][newY] = true;
                    vector<Point> newPath = currentPath;
                    newPath.push_back(Point(newX, newY));
                    q.push({ Point(newX, newY), newPath });
                }
            }
        }
    }

    void printPath() {
        for (int i = path.size() - 1; i >= 0; --i) {
            cout << "(" << path[i].x + 1 << "," << path[i].y + 1 << ") " << endl;
        }
    }

    ~MG() {
        for (int i = 0; i < m; i++) {
            delete[] S[i];
        }
        delete[] S;
    }
};

int main() {
    int m, n;
    cin >> m >> n;
    MG m1(m, n);
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/900472.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

数据采集与数据分析:数据时代的双轮驱动

“在当今这个数据驱动的时代&#xff0c;信息已成为企业决策、市场洞察、科学研究等领域不可或缺的核心资源。而爬虫数据采集与数据分析&#xff0c;作为数据处理链条上的两大关键环节&#xff0c;它们之间相辅相成&#xff0c;共同构成了数据价值挖掘的强大引擎。” 爬虫数据采…

【js逆向专题】12.RPC技术

目录 一. websocket1. 什么是websocket2. websocket的原理3. websocket实现方式1. 客户端2.服务端3. 实际案例1. 案例目标2. 解析思路 二. RPC1. RPC 简介2.Sekiro-RPC1. 使用方法1. 执行方式2.客户端环境3.使用参数说明 2. 测试使用1. 前端代码2. SK API3.python调用代码 三.项…

AR模型时序预测——预测未来(含完整代码)

一、前言 随着数据科学的快速发展&#xff0c;利用自回归&#xff08;AR&#xff09;模型进行时序预测已成为一个热门话题。AR模型因其简洁有效&#xff0c;广泛应用于各类预测任务。本文将介绍一套基于Matlab的AR模型时序预测代码&#xff0c;重点在于如何通过历史数据预测未…

工业相机详解及选型

工业相机相对于传统的民用相机而言&#xff0c;具有搞图像稳定性,传输能力和高抗干扰能力等&#xff0c;目前市面上的工业相机大多数是基于CCD&#xff08;Charge Coupled Device)或CMOS(Complementary Metal Oxide Semiconductor)芯片的相机。 一&#xff0c;工业相机的分类 …

爬虫+数据保存

爬虫以及数据保存 这篇文章, 分享如何将爬虫爬到的数据, 保存到excel表格当中。 文章目录 1.安装保存数据的第三方库openpyxl并使用 2.爬虫加单表数据保存 3.爬虫加多表数据保存 4.实战 一、安装保存数据的第三方库openpyxl并使用 我们需要安装openpyxl的第三方库 安装…

01 springboot-整合日志(logback-config.xml)

logback-config.xml 是一个用于配置 Logback 日志框架的 XML 文件&#xff0c;通常位于项目的 classpath 下的根目录或者 src/main/resources 目录下。 Logback 提供了丰富的配置选项&#xff0c;可以满足各种不同的日志需求。需要根据具体情况进行配置。 项目创建&#xff0…

打造充电场站:场地选择与合规运营详解

建设一座充电站需要六步流程&#xff1a;准备工作 → 备案 → 土地审核 → 规划审核 → 电力申请 → 验收确认 一、准备工作 在确定建设前&#xff0c;要考察待选的场地&#xff0c;例如空地、停车场等&#xff0c;与场地所有方签订充电站建设合作协议。根据场地和车流量等实际…

用docker Desktop 下载使用thingsboard/tb-gateway

1、因为正常的docker pull thingsboard/tb-gateway 国内不行了&#xff0c;所以需要其它工具来下载 2、在win下用powershell管理员下运行 docker search thingsboard/tb-gateway 可以访问到了 docker pull thingsboard/tb-gateway就可以下载了 3、docker Desktop就可以看到…

铲屎官进!双十一宠物空气净化器买哪款,有什么推荐的吗?

害&#xff0c;一到换毛季&#xff0c;真的顶不顺&#xff01;家里两只布偶疯狂掉毛&#xff0c;地板、衣服上这些常规的地方就不用说了&#xff0c;竟然连水杯旁也有浮毛的存在&#xff0c;被我不小心喝进去好几次&#xff0c;最严重的时候已经猫毛拌饭了。 我寻求了很多解决方…

jQuery:动画 节点

jQuery&#xff1a;动画 & 节点 定位获取位置滚动距离 动画显示隐藏淡入淡出展开收起动画队列自定义动画动画回调函数动画延迟 节点插入节点删除节点 定位 获取位置 jquery提供了两个方法&#xff0c;来获取元素所处的位置&#xff1a; // 取值 jQuery对象.offset() // …

【JVM】—深入理解ZGC回收器—背景概念回收流程

深入理解ZGC回收器—背景概念&回收流程 ⭐⭐⭐⭐⭐⭐ Github主页&#x1f449;https://github.com/A-BigTree 笔记链接&#x1f449;https://github.com/A-BigTree/Code_Learning ⭐⭐⭐⭐⭐⭐ 如果可以&#xff0c;麻烦各位看官顺手点个star~&#x1f60a; 文章目录 深入…

采集QQ群成员的过程中遇到的问题

错误思路一&#xff1a;通过抓取windows的QQ软件来获取QQ成员 难点&#xff1a;通过spy获取不到节点和句柄 正确思路&#xff1a;通过抓取手机版本的QQ来获取QQ成员 用到的开发工具 开维控制精灵 按键精灵助手 查找节点 有自带的函数,比如cs控件类cs.id 能提取所有节点js…

基于KV260的基础视频链路通路(MIPI+Demosaic+VDMA)

目录 1. 简介 1.1 要点 1.2 背景 1.2.1 Got stuck 1.2.2 Cant be Initialized 2. Overlay 2.1 参考 Overlay 2.1.1 KV260 Base 2.1.2 Pynq-CV-OV5640 2.2 自建 Overlay 2.2.1 IIC IP 2.2.2 MIPI CSI-2 Rx 2.2.3 AXI4-S Subset 2.2.4 Demosaic 2.2.5 Pixel Pack …

非个人小程序注册材料及认证流程

一、注册材料 1、 电子邮箱A、 未被微信公众平台注册B、 未被微信开放平台注册C、 未被个人微信号绑定过&#xff0c;如果被绑定了需要解绑 或 使用其他邮箱&#xff08;如已被占用建议找回账号登录或换邮箱注册&#xff09;2、 管理员手机号码3、 管理员个人身份证&#xff08…

小程序云开发CMS新版数据模型讲解,可视化网页管理后台,内容管理对数据库进行增删改查操作,新闻小程序实战学习

一直跟着石头哥学习小程序开发的同学比较清楚cms是什么&#xff0c;cms就是可以进行可视化的管理云开发数据库的网页后台。有了cms我们可以很方便的管理云开发数据库。 但是云开发官方一直改版&#xff0c;所以现在cms功能被整合到了云开发的数据模型里&#xff0c;也就是现在想…

opencv 图像翻转- python 实现

在做图像数据增强时会经常用到图像翻转操作 flip。 具体代码实现如下&#xff1a; #-*-coding:utf-8-*- # date:2021-03 # Author: DataBall - XIAN # Function: 图像翻转import cv2 # 导入OpenCV库path test.jpgimg cv2.imread(path)# 读取图片 cv2.namedWindow(image,1) …

第十一章 TypeScript模块和命名空间的介绍和使用

文章目录 一、模块1. 导出基础导出重新导出导出重命名 2. 导入基础导入导入重命名 3. 默认导出4. 模块化兼容exports import require()编译结果 二、命名空间1. 例子2. 命名空间3. 引入命名空间 三、模块和命名空间 一、模块 JavaScript 在 ES2015 中引入了模块的概念&#x…

【331】基于Springboot的“有光”摄影分享网站系统

“有光”摄影分享网站设计与实现 摘 要 自互联网的发展至今&#xff0c;其基础理论与技术都已完善&#xff0c;并积极参与了整个社会各个领域。它容许信息根据媒体传播&#xff0c;并和信息可视化工具一起为大家提供优质的服务。对于信息多头管理、差错率高、信息安全系数差、…

【GAMES101笔记速查——Lecture 18 Advanced Topics in Rendering】

目录 1 渲染前沿 1.1 有偏vs无偏 1.2 无偏光线传播方法&#xff08;Unbiased light transport methods&#xff09; 1.2.1 双向路径追踪&#xff08;Bidirectional path tracing&#xff0c;BDPT&#xff09; &#xff08;1&#xff09;双向路径追踪(BDPT)举例 1.2.2 Metr…

《等保测评新视角:安全与发展的双赢之道》

在数字化转型的浪潮中&#xff0c;企业面临的不仅是技术革新的挑战&#xff0c;更有信息安全的严峻考验。等保测评&#xff0c;作为国家网络安全等级保护的一项重要措施&#xff0c;不仅为企业的安全护航&#xff0c;更成为推动企业高质量发展的新引擎。本文将从全新的视角&…