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

news2024/10/22 7:45:02

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;
}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Vue--数据代理

Object.defineProperty const person {}; // 创建一个新对象 person.name‘xusx’ // 通过 defineProperty 使用数据描述符添加对象属性的示例 Object.defineProperty(person, “age”, { value: 37, // 默认值 writable: true, // 不可写 默认false enumerable: true, // 是…

【数据库系统概论】第3章 关系数据库标准语言SQL(一)数据定义(超详细)

教材&#xff1a; 数据库系统概论&#xff08;第6版&#xff09;王珊,杜小勇,陈红编著 目录 一、SQL概述 1.1 SQL 的产生与发展 1.2 SQL的特点 1.3 SQL的基本概念 二、数据定义 2.1 数据库的定义 2.2 数据表的定义 2.3 模式的定义 一、SQL概述 1974年IBM为关系DBMS设…

3.cpp基本数据类型

cpp基本数据类型 1.cpp基本数据类型 1.cpp基本数据类型 C基本数据类型和C语言的基本数据类型差不多 注意bool类型&#xff1a;存储真值 true 或假值 false&#xff0c;C语言编译器C99以上支持。 C语言的bool类型&#xff1a;要添加 #include <stdbool.h>头文件 #includ…

【C++ 同余 裴蜀定理 中位数贪心 并集查找】2607. 使子数组元素和相等|2071

本文涉及知识点 C贪心 数论&#xff1a;质数、最大公约数、菲蜀定理 C图论 LeetCode2607. 使子数组元素和相等 给你一个下标从 0 开始的整数数组 arr 和一个整数 k 。数组 arr 是一个循环数组。换句话说&#xff0c;数组中的最后一个元素的下一个元素是数组中的第一个元素&a…

windows系统中,在cmd窗口演练 Redis 基本操作命令

文章目录 一、Redis 介绍1.1 Redis 的应用场景1.2 Redis 的特点 二、Windows版Redis安装三、Redis Desktop Manager安装四、Redis 常用基本操作4.1 查看操作4.2 操作string类型的命令4.2.1 设置获取Key4.2.2 MSET&#xff08;Multi&#xff09;支持批量设置key、MGET支持批量获…

擎创科技声明

近日&#xff0c;我司陆续接到求职者反映&#xff0c;有自称是擎创科技招聘人员&#xff0c;冒用“上海擎创信息技术有限公司”名义&#xff0c;用“126.com”的邮箱向求职者发布招聘信息&#xff0c;要求用户下载注册APP&#xff0c;进行在线测评。 对此&#xff0c;我司郑重…

前端算法合集-2(含面试题-美团一面)

主要考察的就是数组扁平化,由浅入深吧 ①利用tostring()和split() let arr [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] let newarrarr.toString().split(,) let numarrnewarr.map((item)>{itemNumber(item)return item }) console.log(numarr) ②利用…

【SpringCloud】Gateway微服务网关(gateway快速⼊⻔ 断⾔⼯⼚ 过滤器⼯⼚ 浏览器同源策略)

Gateway微服务网关 Gateway服务网关什么是Gateway网关&#xff1f;为什么需要网关&#xff1f; Gateway快速入门创建gateway服务&#xff0c;引入依赖编写配置文件yaml和路由规则重启测试 断言工厂过滤器工厂路由过滤器的种类请求头过滤器默认过滤器全局过滤器【可写自定义逻辑…

IT监控(基础篇):好的it监控系统具备什么特点?

好的IT监控系统&#xff0c;应该是什么样的呢&#xff1f; 在当今数字化时代&#xff0c;IT系统已成为企业运营的命脉。因此&#xff0c;一个高效、可靠的IT监控平台对于确保业务连续性和提高运维效率至关重要。那么&#xff0c;一个好的IT监控平台究竟应该具备哪些特点呢&…

Adobe Acrobat DC 打印PDF文件,没有打印出注释的解决方法

adobe acrobat在打印的时候&#xff0c;打印不出来注释内容&#xff08;之前一直可以&#xff0c;突然就不行&#xff09;&#xff0c;升级版本、嵌入字体等等都试过&#xff0c;也在Google找了半天和问了GPT也么找着办法。 无奈之下&#xff0c;自己通过印前检查&#xff0c;…

ARL 灯塔 | CentOS7 — ARL 灯塔搭建流程(Docker)

关注这个工具的其它相关内容&#xff1a;自动化信息收集工具 —— ARL 灯塔使用手册 - CSDN 博客 灯塔&#xff0c;全称&#xff1a;ARL 资产侦察灯塔系统&#xff0c;有着域名资产发现和整理、IP/IP 段资产整理、端口扫描和服务识别、WEB 站点指纹识别、资产分组管理和搜索等等…

Elasticsearch是做什么的?

初识elasticsearch 官方网站&#xff1a;Elasticsearch&#xff1a;官方分布式搜索和分析引擎 | Elastic Elasticsearch是做什么的&#xff1f; Elasticsearch 是一个分布式搜索和分析引擎&#xff0c;专门用于处理大规模数据的实时搜索、分析和存储。它基于 Apache Lucene …

Spring MVC 原理与源码

Spring MVC 整体代码量有 5w 行&#xff0c;通过本专栏&#xff0c;可以快速的研读核心部分的代码&#xff0c;节省你的时间。 DispatcherServlet 的流程处理如下图&#xff1a; 但是随着前后端分离&#xff0c;后端大多提供 Restful API &#xff0c;里面的 ViewResolver 和 …

监控易监测对象及指标之:Kafka中间件JMX监控指标解读

监控易作为一款功能强大的监控软件&#xff0c;旨在为企业提供全方位的IT系统监控服务。其中&#xff0c;针对Kafka中间件的JMX监控是监控易的重要功能之一。本文将详细解读监控易中Kafka的JMX监控指标&#xff0c;帮助企业更好地理解并运用这些数据进行系统性能调优和故障排查…

onlyoffice docker启用jwt并生成jwt

一、说明 本文是docker教程&#xff0c;linux/win的安装版本也类似&#xff0c;只需要修改配置文件中的secrt就可以了【Configuring JWT for ONLYOFFICE Docs - ONLYOFFICE】 二、正文开始 docker启动时候如果不想使用jwt&#xff0c;加上参数-e JWT_ENABLEDfalse就可以了&…

软件I2C的代码

I2C的函数 GPIO的配置——scl和sda都配置为开漏输出 void MyI2C_Init(void) {RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);GPIO_InitTypeDef GPIO_InitStruture;GPIO_InitStruture.GPIO_Mode GPIO_Mode_Out_OD;GPIO_InitStruture.GPIO_PinGPIO_Pin_10 | GPIO_Pin_…

Maven 项目管理工具

目录 Maven简介 Maven快速上手 Maven详细介绍 Maven工作机制 Maven安装及配置 使用IDEA创建Maven Web工程 Maven简介 Maven是 Apache 开源组织奉献的一个开源项目&#xff0c;可以翻译为“专家”或“内行”。 Maven 的本质是一个项目管理工具&#xff0c;将项目开发和管…

Ansible自动化工具

一、Ansible概述 1.1 什么是Ansible Ansible 是一个开源的自动化工具&#xff0c;用于配置管理、应用程序部署和任务自动化。它让你可以通过编写简单的 YAML 文件&#xff08;剧本&#xff0c;Playbooks&#xff09;&#xff0c;轻松管理和配置多个服务器。Ansible 的特点是无…

爬虫逆向-js进阶(续写,搭建网站)

1.搭建简单网站1 from flask import Flask,render_template import requests import json app Flask(name)# **location**的温度是**temp**度&#xff0c;天气状况&#xff1a;**desc**app.route(/) # 绑定处理函数 def index_url():location 101010100data get_weather(lo…

Rust语言编程环境的安装

简介 Rust是一门系统编程语言,专注于安全,尤其是并发安全,支持函数式和命令式以及泛型等编程范式的多范式语言。 Rust语言的特点 系统级编程:Rust语言非常适合进行底层系统级编程,如操作系统、网络协议栈、设备驱动程序等。 内存安全:Rust使用所有权(ownership)系统来…