题解 | #数组元素反转#
数组元素反转
http://www.nowcoder.com/practice/8c9793ae96974a9ebb153d90ef31d357
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] << ", ";
}
// write your code here......
int newArr[6] = {NULL};
// int mid = len / 2;
for(int i = 0;i<=len;i++){
newArr[i] = arr[len- i -1]; //将旧数组的倒序复制给新数组
}
cout << "[";
for (int i = 0; i < len; i++) {
if (i == len - 1) {
cout << newArr[i] << "]" << endl;
break;
}
cout << newArr[i] << ", ";
}
return 0;
}