數據結構測試模擬題(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.pswp.cn/news/906406.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/906406.shtml
英文地址,請注明出處:http://en.pswp.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 區域可以設置變量…