1
0
forked from newde/homework

Compare commits

..

5 Commits

Author SHA1 Message Date
233b470060 上傳檔案到「显示数组」
用函数来实现显示数据
2026-02-06 21:26:51 -05:00
af5b0227a8 上傳檔案到「260202」 2026-02-04 18:24:29 -05:00
058c3364ea 刪除 260202/快速排序.cpp 2026-02-04 18:24:16 -05:00
2502467ed4 上傳檔案到「260202」 2026-02-04 18:23:49 -05:00
d5d141931f 更新 260202/冒泡排序优化版.cpp 2026-02-03 23:24:48 -05:00
3 changed files with 71 additions and 3 deletions

View File

@@ -21,15 +21,17 @@ int main() {
}
}
// 3. 关键判断如果这一轮走完swapped 还是 false说明没有发生任何交换
// 3. 关键判断: 如果这一轮走完swapped 还是 false说明没有发生任何交换
// 这意味着剩下的数字已经全都有序了,直接跳出循环
if (swapped == false) {
if (!swapped) {
cout<<"我没有运行 哈哈!";
break;
}
}
cout << "优化后的排序结果: ";
for (int i = 0; i < n; i++) cout << a[i] << " ";
for (int i = 0; i < n; i++)
cout << a[i] << " ";
return 0;
}

27
260202/快速排序.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include<iostream>
using namespace std;
int main(){
int n;
int a[n];
cout<<"请输入数组的长度:"
cin >> n;
//给数组赋值;
for (int i=0;i<n;i++)
cin >> a[i];
// 选择排序:给第 i 个位置找最合适的(最小的)数
for (int i = 0; i < n - 1; i++) {
int minidx = i; // 1. 先记下当前位置
for (int j = i + 1; j < n; j++) {
if (a[j] < a[minidx]) { // 2. 发现更小的了
minidx = j; // 3. 只是记下它的位置,不交换
}
}
swap(a[i], a[minidx]); // 4. 全看完后,只换这一次
}
//输出已排好的队伍
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}

39
显示数组/showstr.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include <iostream>
#include <string>
using namespace std;
struct Harem
{
int id;
int age;
string name;
};
void show(const Harem &hm) // ⭐ 类型必须写
{
cout <<" " <<hm.id <<" "<< hm.age <<" "<< hm.name << endl;
// cout << "ID:" << hm.id
// << " AGE: " << hm.age
// << " NAME: " << hm.name
// << endl;
}
int main()
{
Harem people[] =
{
{1001, 18, "佐伊"},
{1002, 19, "寒冰"},
{1003, 17, "女警"},
{1004, 18, "盖伦"}
};
int size = sizeof(people) / sizeof(people[0]);
for (int i = 0 ; i< size ; i++ )
{
show(people[i]);
}
return 0;
}