0002
反转数组
#include<iostream>
using namespace std;
int main() {
//数组元素逆置
//创建数组
int a[8] = { 1,3,5,6,7,9,66,45};
for (int i = 0; i < 8; i++) {
cout << "数组元素:" << a[i] << endl;
}
//数组元素个数求解 sizeof(a)/sizeof(a[0])-1
//逆置
//首元素
int low = 0;
//末尾元素
int high = sizeof(a) / sizeof(a[0]) - 1;
//交换
while (low < high) {
int temp = 0;
temp = a[low];
a[low] = a[high];
a[high] = temp;
low++;
high--;
}
//打印逆置数组
for (int i = 0; i < 8; i++) {
cout << a[i] << endl;
}
system("pause");
return 0;
}
0003
// 冒泡排序 这个写的不太好,有点四班
#include<iostream>
using namespace std;
int main() {
//冒泡排序
//创建数组
int a[8] = {1,3,5,6,7,9,66,45};
//打印初始序列
cout << "排序前" << endl;
for (int i = 0; i <8; i++) {
cout << a[i] << " " ;
}
cout << endl;
//冒泡
for (int i = 0; i < 8 - 1; i++) {
//内层循环
//对比次数:元素次数-轮数-1
for (int j = 0; j < 8 - i - 1; j++) {
//交换
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
//输出排序后的数组
cout << "排序后" << endl;
for (int i = 0; i < 8; i++) {
cout << a[i] << " " ;
}
cout << endl;
system("pause");
return 0;
}