如一个5X5的矩阵,然后对其进行逆序。
1
2
3
4
5
10
15
20
25
24
23
22
21
16
11
6
7
8
9
14
19
18
17
12
13
思想,每次一层矩阵的外边缘进行遍历。
`#include
#include
#include
#define N 5
using namespace std;
vector getedge(vector matrix,int n)
{
int col=sqrt(matrix.size());
int ccol=sqrt(matrix.size())-2n;
vectoredge;
int i;
for (i=coln+n;i<col*n+n+ccol;i++){
printf ("%d\n",matrix[i]);
edge.push_back(matrix[i]);
}
i=i+col-1;
for (int j=0;j<ccol-1;i=i+col,j++)
{
printf ("%d\n",matrix[i]);
edge.push_back(matrix[i]);
}
i=i-col-1;
int j=i;
for (;i>j-ccol+1;i--)
{
printf ("%d\n",matrix[i]);
edge.push_back(matrix[i]);
}
i=i-col+1;
for (;i>col*n+n;i=i-col)
{
printf ("%d\n",matrix[i]);
edge.push_back(matrix[i]);
}
return edge;
}
int main()
{
vector<vector<int> > test;
vector <int >c;
for (int i=0;i<N;i++)
for (int j=0;j<N;j++)
c.push_back(i*N+j+1);
int num=0;
if((int)sqrt(c.size())%2==0)
num=sqrt(c.size())/2;
else
num=sqrt(c.size())/2+1;
for (int i=0;i<num;i++){
test.push_back(getedge(c,i));
//printf("%d",test.size());
}
return 0;
} `