黑马程序员c++学习笔记
- 入门级逆置数
图解:
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1,2,3,5,4};//设置一个数组
int temp;//设置一个临时存放的区域
int start = 0;//数组开始索引
int end = sizeof(arr) / sizeof(arr[0]) - 1;//数组总长度,也是结尾索引
while (start<end) {
temp = arr[start];//将start索引对应的数组值赋予给临时存放区域
arr[start] = arr[end];//将end索引对应的数组值赋予给start索引对应的数组值
arr[end] = temp; //将临时存放的值赋予给end索引对应的数组值区域
//向中间移动start和end指向的位置
start++;
end--;
}
for(int i=0; i < 5; i++) {//遍历循环展现结果
cout << arr[i]<<endl ;
}
system("pause");
return 0;
}