c++课后题(2) 数组排序
2.排序算法:
输入: 一个数组
1. 冒泡排序
2. 选择排序 (每次选择剩余序列中最小的数)
#include <iostream> #include <iomanip> using namespace std; const int SIZE_LEN=10; void bulb_sort(int [], int ); void select_sort(int [], int); void display_array(int [], int); // 冒泡排序 选择排序 int main(int argc, char *argv[]) { int a[SIZE_LEN]; cout << "Enter ten numbers: " << endl; for(int i=0; i<SIZE_LEN; i++) { cin >> a[i]; } cout << "Enter end" << endl; //bulb_sort(a, SIZE_LEN); select_sort(a, SIZE_LEN); display_array(a, SIZE_LEN); return 0; } void bulb_sort(int seq[], int SIZE) // 冒泡排序 { //int len = sizeof(seq) / sizeof(int); //bool ischanged = true; while(true) { bool ischanged = false; // 记录数据是否发生了调换 for(int i=0; i<SIZE-1; i++) { if(seq[i]>seq[i+1]) // 交换相邻的值 { int temp = seq[i]; seq[i] = seq[i+1]; seq[i+1] = temp; ischanged = true; } } if(!ischanged) // 知道数据不再发生调换 break; } } void select_sort(int seq[], int SIZE) { for(int i=0; i<SIZE-1; i++) { int min_num = seq[i]; int min_index = i; for(int j=i; j<SIZE; j++) { if(seq[j]<min_num) { min_num = seq[j]; // 更新最小值 min_index = j; // 最小值的索引 } } int tmp = seq[i]; //最小值与最小位置的数进行交换 seq[i] = seq[min_index]; seq[min_index] = tmp; } } void display_array(int seq[], int SIZE) // c++将数组作为参数,实际传递的是指针 { for(int i=0; i<SIZE; i++) { cout << setw(4) << seq[i]; } cout << endl; }
储物柜问题:
问题描述:100个柜子,初始时都关闭,第一个人打开所有的,第二个人从第二个开始,每个两个关闭,从第三个人开始,后续来的第n个人,从第n个柜子开始,每隔n个柜子改变一次状态。最后开着的柜子:
#include <iostream> #include <iomanip> using namespace std; const int SIZE_LEN=100; int reversr_state(int); int main(int argc, char *argv[]) { int a[SIZE_LEN] = {0}; for(int i=0; i<SIZE_LEN; i++) { if(i==0) { for(int j=0; j<SIZE_LEN; j++) a[j] = 1; // 第一个学生吧所有的柜子门打开 } else if(i==1) // 第二个学生从第二个柜子开始,每个两个柜子关闭 { for(int j=i; j<SIZE_LEN; j=j+2) a[j] = 0; } else // 后面的第n个学生,每隔n个柜子改变柜子的状态 { for(int j=i; j<SIZE_LEN; j=j+i) { a[j] = reversr_state(a[j]); } } } cout << "The following closet is open: " << endl; int counter = 0; for(int i=0; i<SIZE_LEN; i++) { if(a[i]==1) { cout << setw(4) << i+1; counter++; if(counter%10==0) cout << endl; } } cout << endl; return 0; } int reversr_state(int num) { if(num==0) return 1; else return 0; }
插入排序:
插入排序的思想是从待排序的序列中选择一个元素,插入到已经有序的元素中,对于整个待排序的序列,一般会认为序列的第一个元素是有序的,从第二个元素开始插入。
#include <iostream> using namespace std; // 插入排序 template<typename T> void insertSort(T list[], int n) { // 插入排序, 参数:list待排序的数组 n数组的大小 for(int i=1; i<n; i++) { int index = -1; // 插入的位置 for(int j=0; j<i; j++) { if(list[i]<list[j]) { index = j; break; } } // 插入操作 if(index>=0) { T temp = list[i]; for(int pos=i; pos>index; pos--) { list[pos] = list[pos-1]; } // 插入元素 list[index] = temp; } } } int main(int argc, char *argv[]) { int a[10] = {4, 32, 23, 11, 9, 1, 89, 98, 1, 0}; insertSort(a, 10); for(int i=0; i<10; i++) { cout << a[i] << " "; } cout << endl; return 0; }
运行结果:
高尔顿盒子问题:
#include <iostream> #include <iomanip> #include <cstdlib> #include <time.h> using namespace std; #define RAND_MAX_FLOAT static_cast<float>(RAND_MAX) const int para = 11; // 道尔顿板的大小 const int para_board_width = 41; const int test_num = 400; void result_display(int [], int); int array_sum(int [], int); void array_disp(int [], int); int main(int argc, char *argv[]) { srand(time(0)); int process[para]; int outcome[para_board_width] = {0}; for(int i=0; i<test_num; i++) // 进行100次实验 { for(int j=0; j<para; j++) // 向下落11次 { double tmp = rand()/RAND_MAX_FLOAT; if(tmp>0.5) // 向右偏 { process[j] = 1; cout << "R"; } else { process[j] = -1; cout << "L"; } } cout << endl; int middle = para_board_width/2; // 道尔顿板的中间位置 int index = array_sum(process, para) + middle; // 边界条件设置 if(index<0) index = 0; if(index>para_board_width-1) index = para_board_width - 1; cout << index << endl; outcome[index] += 1; } array_disp(outcome, para_board_width); result_display(outcome, para_board_width); //int c[9] = {0,3,0,3,3,0,3,0,1}; //result_display(c, 9); //cout << array_sum(c, 9) << endl; //array_disp(c, 9); //return 0; } void result_display(int array[], int SIZE_LEN) // 输出最后的结果 { int max = 0; // 找到数组的最大值 for(int i=0; i<SIZE_LEN; i++) { if(array[i]>max) max = array[i]; } // max代表最终要显示多少行 for(int i=0; i<max; i++) // 行数 { for(int j=0; j<SIZE_LEN; j++) { int delta = max - array[j]; if(i>=delta) { cout << "o"; } else { cout << " "; } } cout << endl; // 换行 } cout << endl; } int array_sum(int array[], int array_size) { int sum = 0; for(int i=0; i<array_size; i++) { sum += array[i]; } return sum; } void array_disp(int array[], int array_size) { for(int i=0; i<array_size; i++) { cout << setw(4) << array[i]; } cout << endl; }
从数组中查找一个姓名,并且删除这个姓名:
#include <iostream> #include <iomanip> #include <cstdlib> #include <time.h> #include <string> using namespace std; void printArray(string*, int); int main(int argc, char *argv[]) { cout << "Enter the capacity: "; int capacity; cin >> capacity; string* p = new string[capacity]; cout << "Enter " << capacity << " names: " << endl; for(int i=0; i<capacity; i++) { cin >> p[i]; } printArray(p, capacity); cout << "Enter the name you want to delete: "; string name; cin >> name; bool found_flag = false; int index; for(int i=0; i<capacity; i++) { if(p[i]==name) { found_flag = true; index = i; break; } } if(found_flag) // 找到了 { for(int k=index; k<capacity-1; k++) { p[k] = p[k+1]; } capacity--; cout << "Name " << name << " is successfully deleted from nameList" << endl; } else { cout << name << " is not in the nameList." << endl; } printArray(p, capacity); return 0; } void printArray(string* nameList, int num) { for(int i=0; i<num; i++) { cout << nameList[i] << endl; } cout << endl; }
------------------------------------------分割线---------------------------------------------------------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)