题解 | #数组元素反转#
数组元素反转
http://www.nowcoder.com/practice/8c9793ae96974a9ebb153d90ef31d357
#include using namespace std;
int main() {
int arr[6] = { 0 };
int len = sizeof(arr) / sizeof(int);
for (int i = 0; i < len; i++) {
cin >> arr[i];
}
cout << "[";
for (int i = 0; i < len; i++) {
if (i == len - 1) {
cout << arr[i] << "]" << endl;
break;
}
cout << arr[i] << ", ";
}
//这段主要是将第一个与最后一个互换,到中间位置停止互换
int a=0;
for(int i=0;i<len/2;i++)
{
a=arr[i];
arr[i]=arr[len-i-1];
arr[len-i-1]=a;
}
cout << "[";
for (int i = 0; i < len; i++) {
if (i == len - 1) {
cout << arr[i] << "]" << endl;
break;
}
cout << arr[i] << ", ";
}
return 0;
}