數據結構測試模擬題(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
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/83302.shtml
英文地址,請注明出處:http://en.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;但是查詢結果…