1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include <utility>
void swapEvenOdd(int a[], const size_t n)
{
if (n%2)
{
for (auto i = 0; i < n - 1; i += 2 )
{
std::swap (a[i], a[i+1]);
}
}
else
{
for (auto i = 0; i < n; i += 2 )
{
std::swap (a[i], a[i+1]);
}
}
for (auto i = 0; i <n; ++i)std::cout << a[i] << " ";
}
int main()
{
int a[] = {1, 2, 3, 4, 5, 6};
swapEvenOdd(a, sizeof(a)/sizeof(a[0]));
std::cout << "\n";
int b[] = {1, 2, 3, 4, 5, 6, 7};
swapEvenOdd(b, sizeof(b)/sizeof(b[0]));
}
|