AcWing 816. 数组翻转
原题链接
简单
作者:
Backkom
,
2021-03-20 14:47:23
,
所有人可见
,
阅读 348
交换1
#include <iostream>
using namespace std;
void reverse(int a[], int size)
{
for (int i = 0; i <= (size - 1) / 2; i ++)
{
int t = a[i];
a[i] = a[size - 1 - i];
a[size - 1 - i] = t;
}
}
int a[1010];
int main()
{
int size, n;
cin >> n >> size;
for (int i = 0; i < n; i ++) cin >> a[i];
reverse(a, size);
for (int i = 0; i < n; i ++) cout << a[i] << ' ';
return 0;
}
双指针交换
#include <iostream>
using namespace std;
void reverse(int a[], int size)
{
for (int i = 0, j = size - 1; i < j ; i ++, j --)
swap(a[i], a[j]);
}
int a[1010];
int main()
{
int size, n;
cin >> n >> size;
for (int i = 0; i < n; i ++) cin >> a[i];
reverse(a, size);
for (int i = 0; i < n; i ++) cout << a[i] << ' ';
return 0;
}