编写函数实现js输出旋转矩阵

比如,参数是2,输出矩阵

1 2
4 3

参数是3,输出:

1 2 3
8 9 4
7 6 5 

请尝试用js编写一个这样的方法!!!求解。

function hehen { var item = new Arrayn; var ceng = []; forvar i = 0; i < n; i ++ { var h = item[i] = new Arrayn; forvar j = 0; j < n; j ++ { var en = Math.mini,j,n-1-j,n-1-i; var hai = 0; forvar k = 0; k < en; k ++ { hai += hahan - en + 1; } h[j] = hai + lalai-en, j-en, n-en*2; } } forvar i = 0; i < n; i ++ { console.logitem[i].join ; } } function lalax,y,n{ ifn < 1 n = 1; ifx === 0{ return y + 1; }else ify === n-1{ return n + x; }else ifx === n-1{ return 3*n - y - 2; }else ify === 0{ return hahan - x + 1; } } function hahax { return x - 1 * 4; }
//调用
hehe10;

function getMatrixn { var matrix = Array.from{length: n }, function {return []}; var num = 1, rowbegin = 0, rowend = n - 1, colbegin = 0, colend = n - 1; while rowbegin <= rowend && colbegin <= colend { for var i = colbegin; i <= colend; i++ matrix[rowbegin][i] = num++; rowbegin++; for var i = rowbegin; i <= rowend; i++ matrix[i][colend] = num++; colend--; for var i = colend; i >= colbegin; i-- matrix[rowend][i] = num++; rowend--; for var i = rowend; i >= rowbegin; i-- matrix[i][colbegin] = num++; colbegin++; } return matrix } console.loggetMatrix3

发表评论

电子邮件地址不会被公开。 必填项已用*标注