数据结构测试模拟题(3)

1、两个有序链表序列的合并

#include<bits/stdc++.h>
using namespace std;struct node{int num;node* next;
};// 创建链表
node* CreatList(){int x;node *head = new node();  // 创建头节点head->next = NULL;node *tail = head;        // 尾指针初始指向头节点while(cin >> x && x != -1){node *p = new node();p->num = x;p->next = NULL;tail->next = p;       // 新节点插入到尾部tail = p;             // 尾指针移动到新节点}return head;
}// 合并两个有序链表
node* ListUnion(node* heada, node* headb){node *pa = heada->next;   // 跳过头节点,指向第一个数据节点node *pb = headb->next;node *dummy = new node(); // 合并后的虚拟头节点node *pc = dummy;while(pa && pb){if(pa->num <= pb->num){pc->next = pa;pa = pa->next;} else {pc->next = pb;pb = pb->next;}pc = pc->next;        // 移动pc指针}// 连接剩余节点pc->next = pa ? pa : pb;// 释放原链表头节点delete heada;delete headb;return dummy;
}// 输出链表
void print(node *head){node *p = head->next;     // 跳过头节点if(!p){                   // 处理空链表cout << "NULL";return;}cout << p->num;           // 输出第一个节点p = p->next;while(p){cout << " " << p->num; // 控制空格输出p = p->next;}cout << endl;
}int main(){node *heada = CreatList();node *headb = CreatList();node *head = ListUnion(heada, headb);print(head);// 释放合并后的链表while(head){node *temp = head;head = head->next;delete temp;}return 0;
}

2、一元多项式的加法运算

#include<bits/stdc++.h>
using namespace std;
struct node{double xishu;int zhishu;
};
int main(){vector<node>p1,p2,result;int n;cin>>n;for(int i=0;i<n;i++){node t;cin>>t.xishu>>t.zhishu;p1.push_back(t);}int m;cin>>m;for(int j=0;j<m;j++){node t;cin>>t.xishu>>t.zhishu;p2.push_back(t);}int i=0,j=0;while(i<n&&j<m){if(p1[i].zhishu>p2[j].zhishu){result.push_back(p1[i]);i++;}else if(p1[i].zhishu<p2[j].zhishu){result.push_back(p2[j]);j++;}else{double sum=p1[i].xishu+p2[j].xishu;if(sum!=0){result.push_back({sum,p1[i].zhishu});}i++;j++;}}while(i<n)result.push_back(p1[i++]);while(j<m)result.push_back(p2[j++]);for(const auto& term: result){cout<<fixed<<setprecision(2)<<term.xishu<<" "<<term.zhishu<<"\n";}return 0;
}

3、栈操作的合法性

#include<bits/stdc++.h>
using namespace std;bool isValid(string s, int m) {int cnt = 0;for(int i = 0; i < s.length(); i++) {char c = s[i];if(c == 'S') {cnt++;if(cnt > m) {return false;  // 超过最大容量}} else {cnt--;if(cnt < 0) {return false;  // 栈空时出栈}}}return cnt == 0;  // 最终栈必须为空
}int main() {int n, m;cin >> n >> m;for(int i = 0; i < n; i++) {string s;cin >> s;if(isValid(s, m)) {cout << "YES" << endl; } else {cout << "NO" << endl;  }}return 0;
}

4、括弧匹配检验

#include<bits/stdc++.h>
using namespace std;bool isValid(string s) {stack<char> st;for(int i = 0; i < s.length(); i++) {char c = s[i];if(c == '(' || c == '[') {st.push(c);} else if(c == ')' || c == ']') {if(st.empty()) {return false;}char top = st.top();st.pop();if((c == ')' && top != '(') || (c == ']' && top != '[')) {return false;}}}return st.empty();
}int main() {string s;cin >> s;if(isValid(s)) {cout << "OK" << endl;} else {cout << "Wrong" << endl;}return 0;
}

5、还原二叉树

#include<bits/stdc++.h>
using namespace std;struct node{char value;node* left;node* right;// 添加构造函数node(char val) : value(val), left(NULL), right(NULL) {}
};int n;node* buildtree(string pre, string idx){if(pre.empty() || idx.empty()){return NULL;}char vroot = pre[0];node* root = new node(vroot);int idxroot = idx.find(vroot);string leftidx = idx.substr(0, idxroot);string rightidx = idx.substr(idxroot+1);string leftpre = pre.substr(1, leftidx.length());string rightpre = pre.substr(1 + leftidx.length());root->left = buildtree(leftpre, leftidx);root->right = buildtree(rightpre, rightidx);return root;
}// 修正函数名和返回值类型
int treeHeight(node* root){if(root == NULL) return 0;int leftheight = treeHeight(root->left);int rightheight = treeHeight(root->right);return max(leftheight, rightheight) + 1;
}int main(){cin >> n;string pre, idx;cin >> pre >> idx;node* root = buildtree(pre, idx); // 构建二叉树int h = treeHeight(root); // 调用修正后的函数名cout << h << endl; return 0;
}

6、求先序排列(后中求先)

#include<bits/stdc++.h>
using namespace std;struct node{char value;node* left;node* right;node(char x):value(x),left(NULL),right(NULL){}
};//post表示后序,idx表示中序
node* buildtree(string post, string idx){if(post.empty() || idx.empty()) return NULL;// 后序遍历的最后一个字符是根节点char rootv = post.back();node* root = new node(rootv);// 在中序遍历中找到根节点的位置int rootidx = idx.find(rootv);// 分割中序遍历为左子树和右子树string leftidx = idx.substr(0, rootidx);string rightidx = idx.substr(rootidx + 1);// 分割后序遍历为左子树和右子树string leftpost = post.substr(0, leftidx.length());string rightpost = post.substr(leftidx.length(), rightidx.length());// 修正递归调用的参数顺序:(后序, 中序)root->left = buildtree(leftpost, leftidx);root->right = buildtree(rightpost, rightidx);return root;
}void xianxu(node* root){if(root == NULL) return;  //直接返回,不返回值cout << root->value;xianxu(root->left);xianxu(root->right);
}int main(){string post, idx;  // 修正变量名以反映实际用途cin >> idx >> post;  //先输入中序,再输入后序node* root = buildtree(post, idx);xianxu(root);cout << endl;return 0;
}

7、迷宫最短路径问题

#include <iostream>
#include <queue>
using namespace std;struct node {int r, c, s; // row column step
};int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int b[15][15];
int a[15][15];
int n;int bfs(int x, int y) {queue<node> mg;node q;q.r = x;q.c = y;q.s = 0;b[q.r][q.c] = 1;mg.push(q);while (!mg.empty()) {q = mg.front();mg.pop();if (q.r == n - 2 && q.c == n - 2) {return q.s;}node t;for (int i = 0; i < 4; i++) {t.r = q.r + dir[i][0];t.c = q.c + dir[i][1];if (!b[t.r][t.c] && a[t.r][t.c] == 0 && t.r >= 0 && t.r < n && t.c >= 0 && t.c < n) {b[t.r][t.c] = 1;t.s = q.s + 1;mg.push(t);}}}return -1;
}int main() {cin >> n;for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {cin >> a[i][j];}}for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {b[i][j] = 0;}}int result = bfs(1, 1);if (result == -1) {cout << "No solution\n";} else {cout << result << "\n";}return 0;
}    

8、图着色问题

#include<bits/stdc++.h>
using namespace std;
const int N=510,M=N*N;
int color[N];
vector<int> g[M];
int v,m,k,n;void add(int a,int b){g[a].push_back(b);g[b].push_back(a);
}
int judge(int cnt){if(cnt!=k)return 0;for(int i=1;i<=v;i++){for(int j=0;j<g[i].size();j++){int t=g[i][j];if(color[i]==color[t])return 0;}}return 1;
}
int main(){cin>>v>>m>>k;while(m--){int a,b;cin>>a>>b;add(a,b),add(b,a);}cin>>n;for(int i=0;i<n;i++){set<int> se;//set具有去重功能for(int j=1;j<=v;j++){cin>>color[j];se.insert(color[j]);} if(judge(se.size()))cout<<"Yes"<<endl;else cout<<"No"<<endl;}return 0;
}

9、地下迷宫探索

#include <bits/stdc++.h>
using namespace std;const int N = 1005;
int vis[N];
int g[N][N];
stack<int> stk;
int n, m, src;// 深度优先搜索函数
void dfs(int k) {vis[k] = 1;if (vis[src]) cout << k;else cout << " " << k;stk.push(k);for (int i = 1; i <= n; i++) {if (!vis[i] && g[k][i] == 1) {cout << " ";dfs(i);}}stk.pop();if(!stk.empty()){cout<<" "<<stk.top();}
}int main() {memset(vis, 0, sizeof(vis));cin >> n >> m >> src; // n代表节点数,m代表边数,src代表初末位置 int s, d;for (int i = 1; i <= m; i++) {cin >> s >> d;g[s][d] = g[d][s] = 1;}dfs(src);bool connected = true;for (int i = 1; i <= n; i++) {if (!vis[i]) {connected = false;break;}}if (!connected) cout << " " << 0;return 0;
}    

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

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

相关文章

LabVIEW Val (Sgnl) 属性

在 LabVIEW 事件驱动架构中&#xff0c;Val (Sgnl) 属性&#xff08;Value (Signaling)&#xff09;是实现编程触发与用户交互行为一致性的关键技术。与普通 Value 属性不同&#xff0c;Val (Sgnl) 在修改控件值的同时强制生成值改变事件&#xff0c;确保程序逻辑与 UI 交互保持…

04.MySQL数据类型详解

MySQL数据类型详解 文章目录 MySQL数据类型数据类型分类数值类型 tinyint类型bit类型float类型decimal类型 字符串类型 char类型varchar类型char和varchar比较 时间日期类型enum和set类型数据类型选择的进阶技巧常见误区与解决方案性能优化与最佳实践 MySQL数据类型 数据类型…

Spring AI 之对话记忆(Chat Memory)

大型语言模型&#xff08;LLMs&#xff09;是无状态的&#xff0c;这意味着它们不会保留关于之前交互的信息。当想在多次交互中保持上下文或状态时&#xff0c;这可能会成为一个限制。为了解决这一问题&#xff0c;Spring AI 提供了对话记忆功能&#xff0c;允许你在与大型语言…

Hölder Statistical Pseudo Divergence Proper Hölder Divergence

目录 Hlder Statistical Pseudo DivergenceProper Hlder Divergence Hlder Statistical Pseudo Divergence Hlder Statistical Pseudo Divergence是一种度量两个概率分布 p p p 和 q q q差异的方法&#xff0c;它基于Hlder不等式。定义如下&#xff1a; D α H ( p : q ) 1 …

时序数据库IoTDB基于云原生的创新与实践

概述 Apache IoTDB 是一款独立自研的物联网时序数据库&#xff0c;作为 Apache 基金会的顶级项目&#xff0c;它融合了产学研的优势&#xff0c;拥有深厚的科研基底。IoTDB 采用了端边云协同的架构&#xff0c;专为物联网设计&#xff0c;致力于提供极致的性能。 数据模型 I…

git 如何解决分支合并冲突(VS code可视化解决+gitLab网页解决)

1、定义&#xff1a;两个分支修改了同一文件的同一行代码&#xff0c;无法自动决定如何合并代码&#xff0c;需要人工干预的情况。&#xff08;假设A提交了文件a,此时B在未拉取代码的情况下&#xff0c;直接提交是会报错的&#xff0c;此时需要拉取之后再提交才会成功&#xff…

系统架构设计师(一):计算机系统基础知识

系统架构设计师&#xff08;一&#xff09;&#xff1a;计算机系统基础知识 引言计算机系统概述计算机硬件处理器处理器指令集常见处理器 存储器总线总线性能指标总线分类按照总线在计算机中所处的位置划分按照连接方式分类按照功能分类 接口接口分类 计算机软件文件系统文件类…

聊一聊接口测试中缓存处理策略

目录 一、强制绕过缓存 添加时间戳参数 修改请求头 二、主动清除缓存 清除本地缓存 清除服务端缓存&#xff08;需权限&#xff09; 清除CDN缓存 三、测试缓存逻辑 首次请求获取数据 记录响应头中的缓存标识​​​​​ 验证缓存生效 测试缓存过期​​​​​​​ 四…

机器学习算法-逻辑回归

今天我们用 「预测考试是否及格」 的例子来讲解逻辑回归&#xff0c;从原理到实现一步步拆解&#xff0c;保证零基础也能懂&#xff01; &#x1f3af; 例子背景 假设你是班主任&#xff0c;要根据学生的「学习时间」预测「是否及格」&#xff0c;手上有以下数据&#xff1a;…

【论文解读】CVPR2023 PoseFormerV2:3D人体姿态估计(附论文地址)

论文链接&#xff1a;https://arxiv.org/pdf/2303.17472 源码链接&#xff1a;https://github.com/QitaoZhao/PoseFormerV2 Abstract 本文提出了 PoseFormerV2&#xff0c;通过探索频率域来提高 3D 人体姿态估计的效率和鲁棒性。PoseFormerV2 利用离散余弦变换&#xff08;DC…

DRW - 加密市场预测

1.数据集描述 在本次比赛中&#xff0c;数据集包含加密市场的分钟级历史数据。您的挑战是预测未来的加密货币市场价格走势。这是一项kaggle社区预测竞赛&#xff0c;您可以以 CSV 文件的形式或通过 Kaggle Notebooks 提交您的预测。有关使用 Kaggle Notebooks 的更多详细信息&a…

嵌入式Linux系统中的启动分区架构

在嵌入式Linux系统架构中,Linux内核、设备树(Device Tree)与引导配置文件构成了系统启动的基础核心。如何安全、高效地管理这些关键文件,直接影响到系统的稳定性与可维护性。近年来,越来越多的嵌入式Linux开发者选择将启动相关文件从传统的“混合存放”方式,转向采用独立…

用户资产化视角下开源AI智能名片链动2+1模式S2B2C商城小程序的应用研究

摘要&#xff1a;在数字化时代&#xff0c;平台流量用户尚未完全转化为企业的数字资产&#xff0c;唯有将其沉淀至私域流量池并实现可控、随时触达&#xff0c;方能成为企业重要的数字资产。本文从用户资产化视角出发&#xff0c;探讨开源AI智能名片链动21模式S2B2C商城小程序在…

Spring是如何实现属性占位符解析

Spring属性占位符解析 核心实现思路1️⃣ 定义占位符处理器类2️⃣ 处理 BeanDefinition 中的属性3️⃣ 替换具体的占位符4️⃣ 加载配置文件5️⃣ Getter / Setter 方法 源码见&#xff1a;mini-spring 在使用 Spring 框架开发过程中&#xff0c;为了实现配置的灵活性&#xf…

【大模型面试每日一题】Day 31:LoRA微调方法中低秩矩阵的秩r如何选取?

【大模型面试每日一题】Day 31&#xff1a;LoRA微调方法中低秩矩阵的秩r如何选取&#xff1f; &#x1f4cc; 题目重现 &#x1f31f;&#x1f31f; 面试官:LoRA微调方法中低秩矩阵的秩r如何选取&#xff1f;&#xff1a; #mermaid-svg-g5hxSxV8epzWyP98 {font-family:"…

字节golang后端二面

前端接口使用restful格式&#xff0c;post与get的区别是什么&#xff1f; HTTP网络返回的状态码有哪些&#xff1f; go语言切片与数组的区别是什么&#xff1f; MySQL实现并发安全避免两个事务同时对一个记录写操作的手段有哪些&#xff1f; 如何实现业务的幂等性&#xff08;在…

Spring Security安全实践指南

安全性的核心价值 用户视角的数据敏感性认知 从终端用户角度出发,每个应用程序都涉及不同级别的数据敏感度。以电子邮件服务与网上银行为例:前者内容泄露可能仅造成隐私困扰,而后者账户若被操控将直接导致财产损失。这种差异体现了安全防护需要分级实施的基本原则: // 伪…

Leetcode第451场周赛分析总结

题目链接 竞赛 - 力扣&#xff08;LeetCode&#xff09;全球极客挚爱的技术成长平台 题目解析 A. 3560. 木材运输的最小成本 AC代码 class Solution { public:long long minCuttingCost(int n, int m, int k) {if (n > m) swap(n, m); // n < m;using ll long lon…

Linux中的shell脚本

什么是shell脚本 shell脚本是文本的一种shell脚本是可以运行的文本shell脚本的内容是由逻辑和数据组成shell脚本是解释型语言 用file命令可以查看文件是否是一个脚本文件 file filename 脚本书写规范 注释 单行注释 使用#号来进行单行注释 多行注释 使用 : " 注释内容…

PHP与MYSQL结合中中的一些常用函数,HTTP协议定义,PHP进行文件编程,会话技术

MYSQL&#xff1a; 查询函数: 执行查询语句: 1.mysql_query("SQL语法"); 凡是执行操作希望拿到数据库返回的数据进行展示的(结果返回: 数据结果); 2.执行结果的处理:成功为结果集&#xff0c;失败为false; 成功返回结果:SQL指令没有错误&#xff0c;但是查询结果…