一个4*4矩阵如何输出

1 2  3  4

5 6  7  8

9 10 11 12

13 14 15 16

一个4*4的矩阵

如何顺时针由外往里输出

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

// 不会要套4个for把?


至尊宝的传说
浏览 966回答 5
5回答

忽然笑

function circle(array) {&nbsp; return array.length < 2 ? array[0] : array.shift().concat(&nbsp; &nbsp; array.map(e=>e.pop()),&nbsp;&nbsp; &nbsp; circle(array.map(e=>e.reverse()).reverse())&nbsp; );}// testcircle([[1,2,3,4],[5,6,7,8],[9,10,11,12]]);

凤凰求蛊

function printMatrix(matrix) {&nbsp; &nbsp; if (matrix == null || matrix.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; var rows = matrix.length;&nbsp; &nbsp; var cols = matrix[0].length;&nbsp; &nbsp; var start = 0;&nbsp; &nbsp; var result = [];&nbsp; &nbsp; while (cols > start * 2 && rows > start * 2) {&nbsp; &nbsp; &nbsp; &nbsp; var endX = cols - 1 - start;&nbsp; &nbsp; &nbsp; &nbsp; var endY = rows - 1 - start;&nbsp; &nbsp; &nbsp; &nbsp; //从左到右打印一行&nbsp; &nbsp; &nbsp; &nbsp; for (var i = start; i <= endX; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.push(matrix[start][i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //从上到下打印一列&nbsp; &nbsp; &nbsp; &nbsp; if (start < endY) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = start + 1; i <= endY; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.push(matrix[i][endX]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //从右到左打印一行&nbsp; &nbsp; &nbsp; &nbsp; if (start < endX && start < endY) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = endX - 1; i >= start; i--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.push(matrix[endY][i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //从下到上打印一列&nbsp; &nbsp; &nbsp; &nbsp; if (start < endX && start < endY - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = endY - 1; i >= start + 1; i--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.push(matrix[i][start]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; start++;&nbsp; &nbsp; }&nbsp; &nbsp; return result}

哆啦的时光机

var test = function (matrix) {&nbsp; &nbsp; var x, y, border = [x = y = 0, matrix.length - 1], result = [];&nbsp; &nbsp; while (x <= border[1] || y <= border[1]) {&nbsp; &nbsp; &nbsp; &nbsp; result.push(matrix[y][x]);&nbsp; &nbsp; &nbsp; &nbsp; if (y == border[0] && x < border[1]) x++;&nbsp; &nbsp; &nbsp; &nbsp; else if (x == border[1] && y < border[1]) y++;&nbsp; &nbsp; &nbsp; &nbsp; else if (y == border[1] && x > border[0]) x--;&nbsp; &nbsp; &nbsp; &nbsp; else if (x == border[0] && y > border[0]) y--;&nbsp; &nbsp; &nbsp; &nbsp; if (x == border[0] && y == border[0]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = x = ++border[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border[1]--;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;}

HUX布斯

原答案var nums = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16";var arr = nums.split(',');for(var i in arr){&nbsp; i-=0;&nbsp; if(i>0&&(i+1)%4==0){&nbsp; &nbsp; &nbsp; var str = arr.slice(i-3,i+1).join(' ');&nbsp; &nbsp; &nbsp; console.log(str)&nbsp; &nbsp; &nbsp;}&nbsp;}我理解错误了

米脂

var arr = [[1, 2,&nbsp; 3,&nbsp; 4],[5, 6,&nbsp; 7,&nbsp; 8],[9, 10, 11, 12],[13, 14, 15, 16]]arr.forEach(row => {&nbsp; &nbsp; row.forEach(n => console.log(n))})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript