数据结构测试模拟题(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、求二叉树的高度

cpp 复制代码
#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、二叉树的遍历

cpp 复制代码
#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≤100

struct 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;
}
相关推荐
平和男人杨争争1 小时前
山东大学计算机图形学期末复习15——CG15
人工智能·算法·计算机视觉·图形渲染
superior tigre1 小时前
C++学习:六个月从基础到就业——C++11/14:其他语言特性
c++·学习
天堂的恶魔9462 小时前
C++ - 仿 RabbitMQ 实现消息队列(2)(Protobuf 和 Muduo 初识)
c++·rabbitmq·ruby
休息一下接着来2 小时前
进程间通信(IPC)常用方式对比
linux·c++·进程间通讯
虾球xz2 小时前
游戏引擎学习第288天:继续完成Brains
c++·学习·游戏引擎
John_ToDebug2 小时前
Chromium 浏览器核心生命周期剖析:从 BrowserProcess 全局管理到 Browser 窗口实例
c++·chrome·性能优化
爱coding的橙子2 小时前
每日算法刷题Day11 5.20:leetcode不定长滑动窗口求最长/最大6道题,结束不定长滑动窗口求最长/最大,用时1h20min
算法·leetcode·职场和发展
WenGyyyL2 小时前
力扣热题——零数组变换 |
算法·leetcode·职场和发展·蓝桥杯
芯眼2 小时前
AMD Vivado™ 设计套件生成加密比特流和加密密钥
算法·fpga开发·集成测试·软件工程
咪嗷喵挖藕哇2 小时前
leetcode 合并区间 java
java·算法·leetcode