### 回答1:
算法1:int a[5] = {1,2,3,4,5};int temp;
for (int i = 0; i < 5 / 2; i++)
{
temp = a[i];
a[i] = a[5 - i - 1];
a[5 - i - 1] = temp;
}算法2:int a[5] = {1,2,3,4,5};int i = 0;
int j = 4;
while (i < j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}算法3:int a[5] = {1,2,3,4,5};int i = 0;
int j = 4;
while (i < j)
{
swap(a[i], a[j]);
i++;
j--;
}
### 回答2:
下面是3种用C++编写的将5个数组元素逆置的算法代码:
算法1:
```cpp
#include<iostream>
using namespace std;
void reverseArray(int arr[], int size){
int temp;
for(int i=0; i<size/2; i++){
temp = arr[i];
arr[i] = arr[size-1-i];
arr[size-1-i] = temp;
}
}
int main(){
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "原数组:";
for(int i=0; i<size; i++){
cout << arr[i] << " ";
}
reverseArray(arr, size);
cout << "\n逆置后的数组:";
for(int i=0; i<size; i++){
cout << arr[i] << " ";
}
return 0;
}
```
算法2:
```cpp
#include<iostream>
using namespace std;
void reverseArray(int arr[], int size){
int start = 0;
int end = size - 1;
while(start < end){
swap(arr[start], arr[end]);
start++;
end--;
}
}
int main(){
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "原数组:";
for(int i=0; i<size; i++){
cout << arr[i] << " ";
}
reverseArray(arr, size);
cout << "\n逆置后的数组:";
for(int i=0; i<size; i++){
cout << arr[i] << " ";
}
return 0;
}
```
算法3:
```cpp
#include<iostream>
#include<algorithm>
using namespace std;
void reverseArray(int arr[], int size){
reverse(arr, arr + size);
}
int main(){
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "原数组:";
for(int i=0; i<size; i++){
cout << arr[i] << " ";
}
reverseArray(arr, size);
cout << "\n逆置后的数组:";
for(int i=0; i<size; i++){
cout << arr[i] << " ";
}
return 0;
}
```
以上3种算法都是将数组元素从两端开始依次交换,实现数组逆置的功能。
### 回答3:
以下是使用 C++ 编写的三种将五个数组元素逆置的算法代码。
方法一:
```cpp
#include <iostream>
#include <algorithm>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
std::reverse(arr, arr + 5);
for (int i = 0; i < 5; i++) {
std::cout << arr[i] << " ";
}
return 0;
}
```
方法二:
```cpp
#include <iostream>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5 / 2; i++) {
int temp = arr[i];
arr[i] = arr[4 - i];
arr[4 - i] = temp;
}
for (int i = 0; i < 5; i++) {
std::cout << arr[i] << " ";
}
return 0;
}
```
方法三:
```cpp
#include <iostream>
void reverse(int* arr, int n)
{
int start = 0;
int end = n - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
reverse(arr, 5);
for (int i = 0; i < 5; i++) {
std::cout << arr[i] << " ";
}
return 0;
}
```
这些代码展示了三种不同的方法来逆置一个包含五个元素的数组。方法一使用 `std::reverse` 函数来逆置数组。方法二使用一个循环来交换数组的前后元素。方法三使用一个自定义的逆置函数来实现同样的效果。