如何用for写一个一样的呢?
#include <iostream>using namespace std;
int main() {
//数组元素逆置;请声明一个5个元素的数组,并且将元素逆置。
int arr = { 1,3,2,5,4 };
cout << "逆置前数组arr:" << endl;
for (int i = 0; i < 5; i++) {
cout << arr << endl;
}
int start = 0;
int end = sizeof(arr) / sizeof(arr) - 1;
do {
int temp = arr;
arr = arr;
arr = temp;
start++; end--;
} while (start < end);
cout << "逆置后数组arr:" << endl;
for (int i = 0; i < 5; i++) {
cout << arr << endl;
}
system("pause");
return 0;
} #include <iostream>
using std::cout, std::endl;
int main()
{
int arr = { 1, 3, 2, 5, 4 }, temp;
for (int i = 0; i < 5 >> 1; i++) {
temp = arr;
arr = arr;
arr = temp;
}
for (const int& num : arr)
cout << num << endl;
return 0;
}
页:
[1]