蛇形矩阵的实现(C++)

/* 也叫螺旋矩阵,如: 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 想刚开始学编程的时候,这个东东居然写不出,现在实现它,算是发泄一下吧。 */ #include using namespace std; #define N 100 struct point{ int xx; int yy; }; class masterray{//蛇形矩阵 private: int matrix[N][N]; int x; int y; int posX; int posY; int value; public: masterray(){} masterray(int n, int m){ x = n; y = m; posX = 0; posY = 0; fun(); } point getPoint(int v, int n, int m);//找出v在蛇形矩阵n*m中的位置 void fun();//蛇形矩阵的小算法 void display();//输出蛇形矩阵 ~masterray(){} }; void masterray::display(){ for(int i=0; iy0; j--){ if(num==value) posX=xn,posY=j; if(num<=x*y) matrix[xn-1][j] = num++; } for(int i=xn-1; i>x0; i--){ if(num==value) posX=i+1,posY=y0+1; if(num<=x*y) matrix[i][y0] = num++; } x0++, y0++; xn--, ym--; } } int main(){ masterray m(7,11); //m.display(); m.getPoint(17,5,5); cout<

你可能感兴趣的:(蛇形矩阵的实现(C++))