Compare commits
10 Commits
ee2503b5a7
...
23f1c1c35e
| Author | SHA1 | Date | |
|---|---|---|---|
| 23f1c1c35e | |||
| cbf7e4467e | |||
| 9604d24ef6 | |||
| 594ab426fd | |||
| f746c7cf86 | |||
| 04e9780b47 | |||
| 1fba558d43 | |||
| 21a96762be | |||
| fd842609c2 | |||
| a6b36e0de5 |
+25
-1
@@ -5,6 +5,7 @@
|
||||
#ifndef MATRIXGRAPH_H
|
||||
#define MATRIXGRAPH_H
|
||||
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
#include<vector>
|
||||
|
||||
@@ -48,7 +49,7 @@ public:
|
||||
|
||||
// 连接顶点
|
||||
void connect(int id1, int id2, T weight = 0) {
|
||||
if (id1 > data.size() || id2 > data.size() || (id1 < 0 || id2 < 0)) {
|
||||
if (id1 >= data.size() || id2 >= data.size() || (id1 < 0 || id2 < 0)) {
|
||||
throw NodeIdOutOfRangeException("您提供的id不合理", 0X001);
|
||||
}
|
||||
if (id1 == id2) {
|
||||
@@ -97,5 +98,28 @@ public:
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
vector<T> DFS(int startID) {
|
||||
if (startID >= matrix.size() || startID < 0) {
|
||||
throw NodeIdOutOfRangeException("您提供的id不合理", 0X004);
|
||||
}
|
||||
vector<bool> visited(matrix.size(), false); //访问标记
|
||||
vector<T> result;
|
||||
|
||||
function<void(int)> dfsHelper = [&](int node) {
|
||||
visited[node] = true;
|
||||
result.push_back(data.at(node));
|
||||
|
||||
for (int i = 0; i < matrix[node].size(); ++i) {
|
||||
if (matrix[node][i].connected && !visited[i]) {
|
||||
dfsHelper(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
dfsHelper(startID);
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
#endif //MATRIXGRAPH_H
|
||||
|
||||
+8
-1
@@ -26,7 +26,7 @@
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
MatrixGraph<int> graph = new MatrixGraph<int>();
|
||||
MatrixGraph<int> graph = MatrixGraph<int>();
|
||||
int n, e;
|
||||
cin >> n >> e;
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -39,5 +39,12 @@ int main() {
|
||||
}
|
||||
cout << "图的邻接矩阵为:" << endl;
|
||||
cout << graph.to_string() << endl;
|
||||
vector<int> dfs = graph.DFS(0);
|
||||
cout << "图的DFS序列为:" << endl;
|
||||
vector<int>::iterator it;
|
||||
for (it = dfs.begin(); it != dfs.end(); it++) {
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
+15
-15
@@ -27,25 +27,25 @@
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
ListGraph<int> graph=ListGraph<int>();
|
||||
int n,e;
|
||||
cin>>n>>e;
|
||||
for(int i=0;i<n;i++) {
|
||||
ListGraph<int> graph = ListGraph<int>();
|
||||
int n, e;
|
||||
cin >> n >> e;
|
||||
for (int i = 0; i < n; i++) {
|
||||
graph.insert(i);
|
||||
}
|
||||
int id1,id2;
|
||||
for(int i=0;i<e;i++) {
|
||||
cin>>id1>>id2;
|
||||
graph.connect(id1,id2);
|
||||
int id1, id2;
|
||||
for (int i = 0; i < e; i++) {
|
||||
cin >> id1 >> id2;
|
||||
graph.connect(id1, id2);
|
||||
}
|
||||
cout<<"邻接表:"<<endl;
|
||||
cout<<graph.to_string()<<endl;
|
||||
cout<<"BFS序列:"<<endl;
|
||||
vector<int> bfs=graph.BFS(0);
|
||||
cout << "邻接表:" << endl;
|
||||
cout << graph.to_string() << endl;
|
||||
cout << "BFS序列:" << endl;
|
||||
vector<int> bfs = graph.BFS(0);
|
||||
vector<int>::iterator it;
|
||||
for(it=bfs.begin();it!=bfs.end();++it) {
|
||||
cout<<*it<<" ";
|
||||
for (it = bfs.begin(); it != bfs.end(); ++it) {
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout<<endl;
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(homework6)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(test1 test1.cpp)
|
||||
add_executable(test2 test2.cpp)
|
||||
@@ -0,0 +1,39 @@
|
||||
// 编写一个程序,输出在顺序表(3,6,2,10,l,8,5,7,4,9) 中采用顺序查找方法查找关键字5的过程。
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
int seqSearch(vector<T> l,T value) {
|
||||
if (l.empty()) {
|
||||
cout << "空表" << endl;
|
||||
return -1;
|
||||
}
|
||||
bool found = false;
|
||||
int index = 0;
|
||||
for (int i=0; i<l.size(); i++) {
|
||||
cout<<"正在顺序查找第"<<i+1<<"位元素"<<"其值为"<<l.at(i)<<endl;
|
||||
|
||||
if (l.at(i) == value) {
|
||||
found = true;
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
cout<<"未查找到:"<<value<<endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
cout<<"查找次数:"<<index+1<<endl;
|
||||
return index;
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<int> l={3,6,2,10,'l',8,5,7,4,9};
|
||||
int index=seqSearch(l,5);
|
||||
cout<<index<<endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// 编写一个程序,输出在顺序表(1,2, 3, 4, 5, 6, 7, 8, 9, 10)中采用折半查找方法查找关键字9的过程。
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
int BinarySearch(vector<T> l, T value, int left, int right) {
|
||||
if (left > right) {
|
||||
return -1; // 未找到目标值
|
||||
}
|
||||
|
||||
int mid = left + (right - left) / 2; // 计算中间位置
|
||||
|
||||
cout << "正在查找第"<<mid+1 <<"位"<<"其值为"<<l.at(mid)<< endl;
|
||||
|
||||
if (l[mid] == value) {
|
||||
return mid; // 找到目标值,返回索引
|
||||
} else if (value < l.at(mid)) {
|
||||
return BinarySearch(l, value, left, mid - 1); // 在左半部分递归查找
|
||||
} else {
|
||||
return BinarySearch(l, value, mid + 1, right); // 在右半部分递归查找
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int BinarySearch(vector<T> l, T value) {
|
||||
return BinarySearch(l,value, 0, l.size() - 1);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
vector<int> l = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
int index=BinarySearch(l, 9);
|
||||
cout<<index<<endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(homework7)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(test1 test1.cpp)
|
||||
add_executable(test2 test2.cpp)
|
||||
@@ -0,0 +1,58 @@
|
||||
// 编写一个程序,实现直接插入排序算法。用相关数据进行测试,并输出各趟的排序结果。
|
||||
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template<typename T>
|
||||
void InsertSort(vector<T> &v) {
|
||||
for (int i = 1; i < v.size(); i++) {
|
||||
T key = v[i];
|
||||
int j = i - 1;
|
||||
|
||||
while (j >= 0 && v[j] > key) {
|
||||
v[j + 1] = v[j];
|
||||
j = j - 1;
|
||||
}
|
||||
v[j + 1] = key;
|
||||
|
||||
cout << "第" << i << "趟排序:" << endl;
|
||||
cout << "插入前:";
|
||||
for (int k = 0; k < j + 1; k++) {
|
||||
cout << v[k] << " ";
|
||||
}
|
||||
cout << "(" << key << ")";
|
||||
for (int k = i + 2; k < i; k++) {
|
||||
cout << v[k] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
cout << "插入后:";
|
||||
for (int k = 0; k < v.size(); k++) {
|
||||
cout << v[k] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<int> v = {1, 3, 2, 4, 6, 5, 7, 10, 9, 8};
|
||||
|
||||
cout << "排序前:" << endl;
|
||||
for (int i = 0; i < v.size(); i++) {
|
||||
cout << v[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
|
||||
InsertSort(v);
|
||||
|
||||
cout << "排序后:" << endl;
|
||||
for (int i = 0; i < v.size(); i++) {
|
||||
cout << v[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// 编写一个程序,实现折半插入排序算法。用相关数据进行测试,并输出各趟的排序结果。
|
||||
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
#include<algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template<typename T>
|
||||
void BinaryInsertSort(vector<T> &v) {
|
||||
for (size_t i = 1; i < v.size(); ++i) {
|
||||
T key = v[i];
|
||||
int low = 0, high = i - 1;
|
||||
while (low <= high) {
|
||||
int mid = low + (high - low) / 2;
|
||||
if (v[mid] > key) {
|
||||
high = mid - 1;
|
||||
} else {
|
||||
low = mid + 1;
|
||||
}
|
||||
}
|
||||
for (int j = i - 1; j >= low; --j) {
|
||||
v[j + 1] = v[j];
|
||||
}
|
||||
v[low] = key;
|
||||
|
||||
cout << "第 " << i << " 趟排序:" << endl;
|
||||
cout << "插入前:";
|
||||
for (int k = 0; k < low; ++k) {
|
||||
cout << v[k] << " ";
|
||||
}
|
||||
cout << "(" << key << ") ";
|
||||
for (int k = low + 1; k < i; ++k) {
|
||||
cout << v[k] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
cout << "插入后:";
|
||||
for (int k = 0; k < v.size(); ++k) {
|
||||
cout << v[k] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<int> v = {1, 3, 6, 4, 2, 5, 7, 10, 9, 8};
|
||||
cout << "排序前:" << endl;
|
||||
vector<int>::iterator it;
|
||||
for (it = v.begin(); it != v.end(); it++) {
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
BinaryInsertSort(v);
|
||||
|
||||
cout << "排序后:" << endl;
|
||||
for (it = v.begin(); it != v.end(); it++) {
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(homework8)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(test1 test1.cpp)
|
||||
add_executable(test2 test2.cpp)
|
||||
@@ -0,0 +1,46 @@
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template<typename T>
|
||||
void ShellSort(vector<T> &v) {
|
||||
int gap = v.size() / 2;
|
||||
|
||||
while (gap > 0) {
|
||||
for (int i = gap; i < v.size(); i++) {
|
||||
T temp = v[i];
|
||||
int j;
|
||||
for (j = i; j >= gap && v[j - gap] > temp; j -= gap) {
|
||||
v[j] = v[j - gap];
|
||||
}
|
||||
v[j] = temp;
|
||||
}
|
||||
cout << "间隔:" << gap << endl << "排序后:";
|
||||
for (int k = 0; k < v.size(); ++k) {
|
||||
cout << v[k] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
gap = gap / 2;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<int> v = {1, 3, 2, 4, 6, 5, 7, 10, 9, 8};
|
||||
vector<int>::iterator it;
|
||||
cout << "排序前" << endl;
|
||||
for (it = v.begin(); it != v.end(); it++) {
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
ShellSort(v);
|
||||
|
||||
cout << "排序后:" << endl;
|
||||
for (it = v.begin(); it != v.end(); it++) {
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// 编写一个程序,实现冒泡排序算法。用相关数据进行测试,并输出各趟的排序结果。
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template<typename T>
|
||||
void BubbleSort(vector<T> &v) {
|
||||
int count = 1;
|
||||
|
||||
for (int i = 0; i < v.size(); i++) {
|
||||
for (int j = 0; j < v.size() - i - 1; j++) {
|
||||
cout << "正在进行第" << count << "次排序,";
|
||||
if (v[j] > v[j + 1]) {
|
||||
cout << "交换" << v[i] << "与" << v[j] << endl;
|
||||
T temp = v[j];
|
||||
v[j] = v[j + 1];
|
||||
v[j + 1] = temp;
|
||||
}
|
||||
cout << "交换后:" << endl;
|
||||
for (int i = 0; i < v.size(); i++) {
|
||||
cout << v[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<int> v = {1, 3, 2, 4, 6, 5, 7, 10, 9, 8};
|
||||
vector<int>::iterator it;
|
||||
|
||||
cout << "排序前:" << endl;
|
||||
for (it = v.begin(); it != v.end(); it++) {
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
BubbleSort(v);
|
||||
|
||||
cout << "排序后:" << endl;
|
||||
for (it = v.begin(); it != v.end(); it++) {
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user