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

1、约瑟夫问题

#include<bits/stdc++.h>
using namespace std;
const int N=25;
int e[N],ne[N],head=-1,idx=1;
int n,m;
void add_to_head(int x){e[idx]=x;ne[idx]=head;head=idx++;
}
void add(int k,int x){e[idx]=x;ne[idx]=ne[k];ne[k]=idx++;
}
int main(){cin>>n>>m;for(int i=1;i<=n;i++){if(i==1){add_to_head(i);}else{add(i-1,i);}}ne[n]=head;int cur=head;while(n--){for(int i=1;i<m-1;i++){cur=ne[cur];}cout<<e[ne[cur]]<<" ";ne[cur]=ne[ne[cur]];cur=ne[cur];}return 0;
}
#include<iostream>
#include<queue>
using namespace std;
int n,m;
int main(){cin>>n>>m;queue<int>q;for(int i=1;i<=n;i++){q.push(i);}int i=1;while(!q.empty()){if(i==m){cout<<q.front()<<" ";q.pop();i=1;}else{q.push(q.front());q.pop();i=i+1;}}return 0;
}

2、单链表的创建,销毁与遍历

#include <iostream>
using namespace std;struct Node {int data;Node* next;
};int main() {Node* head = NULL;int num;// 逆序创建链表while (cin >> num && num != -1) {Node* newNode = new Node;newNode->data = num;newNode->next = head;head = newNode;}// 遍历链表输出Node* cur = head;while (cur != NULL) {cout << cur->data;if (cur->next != NULL) cout << " ";cur = cur->next;}cout << endl;// 销毁链表while (head != NULL) {Node* temp = head;head = head->next;delete temp;}return 0;
}

3、最大子段和

#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;int a[105]; for (int i = 1; i <= n; i++) {cin >> a[i];}int dq_max = a[1]; int qj_max = a[1];for (int i = 2; i <= n; i++) {dq_max = max(a[i], dq_max + a[i]);qj_max = max(qj_max, dq_max);}cout << qj_max << endl;return 0;
}

4、求二叉树的高度

#include<bits/stdc++.h>
using namespace std;
const int N=105;
struct node{int data;int left;int right;
}a[N];
int n;
int dfs(int root){if(root==0)return 0;int h1=dfs(a[root].left);int h2=dfs(a[root].right);return max(h1,h2)+1;
}
int main(){cin>>n;for(int i=1;i<=n;i++){cin>>a[i].data>>a[i].left>>a[i].right;}cout<<dfs(1);return 0;
}

5、二叉树的遍历

#include<bits/stdc++.h>
using namespace std;
const int N=105;
struct node{int left;int right;
}a[N];
int n;
void inorder(int root){if(root==0)return;cout<<root<<" ";inorder(a[root].left);inorder(a[root].right);
}
int main(){cin>>n;for(int i=1;i<=n;i++){cin>>a[i].left>>a[i].right;}inorder(1);return 0;
}

6、完全二叉树的权值

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N];
int n,x;
int main(){int h=0,nn=0;cin>>n;for(int i=1;i<=n;i++){if(i>nn){nn+=pow(2,h);h++;}cin>>x;a[h]+=x;}int max_a=0,maxh=0;for(int i=1;i<=h;i++){if(a[i]>max_a){max_a=a[i];maxh=i;}}cout<<maxh<<endl;return 0;
}

7、二叉树求值

#include<bits/stdc++.h>
using namespace std;
const int N = 105; // 题目限制n≤100struct Node {int value;       // 节点权值int left, right; // 左右子节点编号int count;       // 子树节点个数int sum;         // 子树权值和
};Node nodes[N];// 递归计算以u为根的子树的节点个数和权值和
void dfs(int u) {if (u == 0) return; // 空节点// 递归处理左右子树dfs(nodes[u].left);dfs(nodes[u].right);// 计算当前子树的节点个数和权值和nodes[u].count = 1; // 自身nodes[u].sum = nodes[u].value;if (nodes[u].left != 0) {nodes[u].count += nodes[nodes[u].left].count;nodes[u].sum += nodes[nodes[u].left].sum;}if (nodes[u].right != 0) {nodes[u].count += nodes[nodes[u].right].count;nodes[u].sum += nodes[nodes[u].right].sum;}
}int main() {int n;cin >> n;// 读取每个节点的信息for (int i = 1; i <= n; i++) {cin >> nodes[i].value >> nodes[i].left >> nodes[i].right;nodes[i].count = 0;nodes[i].sum = 0;}// 从根节点开始递归计算(假设根节点是1)dfs(1);// 输出每个节点的子树信息for (int i = 1; i <= n; i++) {cout << nodes[i].count << " " << nodes[i].sum << endl;}return 0;
}

8、最大连续子序列

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int a[N];
int b[N];
int n;
int main(){cin>>n;for(int i=1;i<+n;i++){cin>>a[i];}int t=0;for(int i=1;i<=n;i++){if(a[i]>=0){t=1;break;}}if(t==0){cout<<a[1]<<" "<<a[n];}b[1]=a[1];int max=b[1],begin=0,end=0;for(int i=1;i<n;i++){if(b[i-1]<0){b[i]=a[i];}else{b[i]=b[i-1]+a[i];}if(b[i]>max){max=b[i];end=i;}}for(begin=end;begin>=0;begin--){if(b[begin<0]){break;}}begin++;cout<<max<<endl;return 0;
}

9、单链表基本操作

#include<bits/stdc++.h>
using namespace std;
struct node{int data;node* next;
};
int n;
int main(){cin>>n;node* head=NULL;node* tail=NULL;//构建初始链表for(int i=1;i<=n;i++){int v;cin>>v;node* newnode=new node{v};if(!head){head=tail=newnode;}else{tail->next=newnode;tail=newnode;}}int m;cin>>m;//处理操作for(int i=1;i<=m;i++){int op,k;cin>>op>>k;if(op==0){int d;cin>>d;if(k==0){node* newnode=new node{d};newnode->next=head;head=newnode;if(!tail){tail=newnode;}}else{node* curr=head;for(int j=1;j<k&&curr;j++){curr=curr->next;}if(curr){node* newnode=new node{d};newnode->next=curr->next;curr->next=newnode;if(tail==curr){tail=newnode;}}}}else if(op==1){if(k==0)continue;if(k==1){if(head){node* tmp=head;head=head->next;if(!head)tail=NULL;delete tmp;}}else{node* p=head;for(int j=1;j<k-1&&p;j++){p=p->next;}if(p&&p->next){node* t=p->next;p->next=t->next;if(t==tail) tail=p; // 修正:更新尾指针delete t;	}}}}//输出链表node* curr=head;while(curr){cout<<curr->data<<" ";curr=curr->next;}cout<<endl;//释放内存while(head){node* tmp=head;head=head->next;delete tmp;}return 0;
}

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

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

相关文章

Helm配置之为特定Deployment配置特定Docker仓库(覆盖全局配置)

文章目录 Helm配置之为特定Deployment配置特定Docker仓库(覆盖全局配置)需求方法1:使用Helm覆盖值方法2: 在Lens中临时修改Deployment配置步骤 1: 创建 Docker Registry Secret步骤 2: 在 Deployment 中引用 Secret参考资料Helm配置之为特定Deployment配置特定Docker仓库(覆…

BERT 作为Transformer的Encoder 为什么采用可学习的位置编码

摘要 BERT 在位置编码上与原始 Transformer 论文中的 sin/cos 公式不同&#xff0c;选择了可学习&#xff08;learned&#xff09;的位置嵌入方案。本文将从 Transformer 原始位置编码选项入手&#xff0c;分析 BERT 选择 learned positional embeddings 的四大核心原因&#x…

【Linux 学习计划】-- gcc、g++、动静态库链接

目录 什么是gcc、g gcc、g 相关操作详解 预处理、编译、汇编、链接来源 动静态链接是什么 结语 什么是gcc、g gcc、g其实就是编译器&#xff0c;是帮助我们从.c或者.cc&#xff0c;.cpp文件编译成可执行程序的 其中&#xff0c;我们如果要编译c语言文件的话&#xff0c;…

前端读取本地项目中 public/a.xlsx 文件中的数据 vue3

前端读取本地项目中 public/a.xlsx 文件中的数据 vue3 项目中需要在 Vue3 项目中读取 public/a.xlsx 文件&#xff0c;可以使用 fetch API 来获取文件内容 一、安装 xlsx 首先&#xff0c;你需要安装 xlsx 库&#xff1a; npm install xlsx二、在需要用的页面里引入xlsx im…

MySQL:to many connections连接数过多

当你遇到 MySQL: Too many connections 错误时&#xff0c;意味着当前连接数已达到 MySQL 配置的最大限制。这通常是由于并发连接过多或连接未正确关闭导致的。 一、查看当前连接数 查看 MySQL 当前允许的最大连接数 SHOW VARIABLES LIKE max_connections;查看当前使用的最大…

2024年热门AI趋势及回顾

人工智能的崛起 2024 年可能会被铭记为人工智能不再是一种技术新奇事物&#xff0c;而是成为现实的一年。微软、Salesforce 和 Intuit 等巨头将人工智能融入主流企业解决方案&#xff1b;从文案写作到数据分析&#xff0c;专门的人工智能应用程序和服务如雨后春笋般涌现&#…

LangFlow技术深度解析:可视化编排LangChain应用的新范式 -(2)流编辑器系统

Flow Editor System | langflow-ai/langflow | DeepWiki 流编辑器系统 相关源文件 流编辑器系统是 Langflow 的核心交互式组件&#xff0c;允许用户直观地创建、编辑和管理 LLM 驱动的应用程序。它提供了一个直观的画布&#xff0c;用户可以在其中添加节点、将其与边缘连接并…

驱动-定时-秒-字符设备

文章目录 目的相关资料参考实验驱动程序-timer_dev.c编译文件-Makefile测试程序-timer.c分析 加载驱动-运行测试程序总结 目的 通过定时器timer_list、字符设备、规避竞争关系-原子操作&#xff0c;综合运用 实现一个程序&#xff0c;加深之前知识的理解。 实现字符设备驱动框…

[Java实战]Spring Boot整合Kafka:高吞吐量消息系统实战(二十七)

[Java实战]Spring Boot整合Kafka&#xff1a;高吞吐量消息系统实战&#xff08;二十七&#xff09; 一、引言 Apache Kafka作为一款高吞吐量、低延迟的分布式消息队列系统&#xff0c;广泛应用于实时数据处理、日志收集和事件驱动架构。结合Spring Boot的自动化配置能力&…

Kotlin Multiplatform--04:经验总结(持续更新)

Kotlin Multiplatform--04&#xff1a;经验总结&#xff08;持续更新&#xff09; 引言 引言 本章用来记载笔者开发过程中的一些经验总结 一、Ktor设置Header 在官方文档中&#xff0c;想要设置Header的示例代码如下&#xff1a; client.get("https://ktor.io&qu…

在 Ubuntu 系统中,将 JAR 包安装为服务

在 Ubuntu 系统中&#xff0c;将 JAR 包安装为服务可以通过 systemd 来实现。以下是详细的操作步骤&#xff1a; 准备工作 确保 JAR 文件路径和 Java 运行时环境已准备好。验证 Java 是否可用&#xff1a; java -version创建 systemd 服务文件 systemd 的服务文件通常位于 …

电商项目-商品微服务-品牌管理微服务开发

一、功能分析 品牌管理微服务包括&#xff1a; &#xff08;1&#xff09;查询全部列表数据 &#xff08;2&#xff09;根据ID查询实体数据 &#xff08;3&#xff09;增加 &#xff08;4&#xff09;修改 &#xff08;5&#xff09;删除 &#xff08;6&#xff09;分页…

Spring Boot开发—— 整合Lucene构建轻量级毫秒级响应的全文检索引擎

文章目录 一、为什么选择 Lucene?轻量级搜索的底层密码二、核心原理:Lucene 的倒排索引2.1 倒排索引:速度之源2.2 段合并优化策略三、Spring Boot集成Lucene实战3.1 依赖配置3.2 实体与索引设计3.3 核心索引服务(含异常处理)3.4 使用示例(测试类)四、高级优化技巧4.1 索…

SpringBootDay1|面试题

目录 一、springboot框架 1、什么是springboot 2、Spring Boot的主要优点 3、springboot核心注解 4、定义banner&#xff08;springboot的logo&#xff09; 5、springboot配置文件 6、springboot 整合 jdbc 二、面试题 1&#xff09;springmvc的作用 ​编辑 2&#x…

jQuery Ajax中dataType 和 content-type 参数的作用详解

jQuery Ajax中dataType与contentType参数解析 一、核心概念对比 参数作用对象数据类型默认值dataType响应数据预期接收的数据格式jQuery自动判断&#xff08;根据响应头MIME类型&#xff09;contentType请求数据发送数据的编码格式application/x-www-form-urlencoded 二、da…

几款常用的虚拟串口模拟器

几款常用的虚拟串口模拟器&#xff08;Virtual Serial Port Emulator&#xff09;&#xff0c;适用于 Windows 系统&#xff0c;可用于开发和调试串口通信应用&#xff1a; 1. com0com (开源免费) 特点&#xff1a; 完全开源免费&#xff0c;无功能限制。 可创建多个虚拟串口…

LLM笔记(六)线性代数

公式速查表 1. 向量与矩阵&#xff1a;表示、转换与知识存储的基础 向量表示 (Vectors): 语义的载体 在LLM中&#xff0c;向量 x ∈ R d \mathbf{x}\in\mathbb{R}^d x∈Rd 是信息的基本单元&#xff0c;承载着丰富的语义信息&#xff1a; 词嵌入向量 (Word Embeddings)&am…

[特殊字符] Word2Vec:将词映射到高维空间,它到底能解决什么问题?

一、在 Word2Vec 之前,我们怎么处理语言? 在 Word2Vec 出现之前,自然语言处理更多是“工程方法”,例如字符串匹配、关键词提取、正则规则...。但这些表示通常缺乏语义,词与词之间看不出任何联系以及非常浅显。当然,技术没有好坏,只有适合的场景。例如: 关键词匹配非常…

栈和队列的模拟实现

栈和队列的模拟实现 容器适配器priority_queue(优先级队列&#xff09;priority_queue的使用priority_queue的模拟实现&#xff1a; 仿函数什么叫仿函数&#xff1f;需要自己实现仿函数的情况&#xff1a; 栈的模拟实现队列的模拟实现deque&#xff08;vector和list的缝合怪&am…

idea本地debug断点小技巧

idea本地debug断点小技巧 简单的设置断点条件 断点后&#xff0c;右键这个断点&#xff0c;可以在 condition 中填写能得出布尔的表达式 a 1 你如果写如下&#xff0c;表示先给他赋值&#xff0c;然后断住 a 2; true 断点后设置某个变量的值 在 debug 区域可以设置变量…