根据矩阵的转置公式,有 cba = (arbrcr)r ,所以首先将abc各部分转置,然后再将整个向量转置。
详细代码如下:
#include <iostream>
using namespace std;
#define MAX 12
int x[MAX];
void new_swap(int i, int j)
{
int t = 0;
while(i <= j)
{
t = x[i];
x[i] = x[j];
x[j] = t;
i++;
j--;
}
}
int main()
{
int n = MAX;
for(int i = 0; i < n; i++)
x[i] = i;
// the numbers Array is rotated
int rotate = 4;
int a = 0;
int c = n - rotate;
// x[a,rotate-1] is swapped
new_swap(a,rotate-1);
// x[rotate, c-1] is swapped
new_swap(rotate, c -1);
// x[c, n-1] is swapped
new_swap(c,n-1);
// x[a,n-1] is swapped
new_swap(a, n-1);
for(int i = 0; i < n; i++)
cout << x[i] <<" ";
cout <<endl;
return 0;
}