题解 | #二维数组操作#
二维数组操作
https://www.nowcoder.com/practice/2f8c17bec47e416897ce4b9aa560b7f4
这道题可以用switch case来解决,完整代码如下:
//新建index变量,初始值为0; let index = 0; //新建m,n变量; let m, n; while (line = readline()) { //每次读取一行字符串,index+1; index += 1; //新建并初始化output值为0; let output = 0; //switch index%5的值 switch (index % 5) { //如index%5 == 1,下面case2, case3。。。模式一样; case 1 : [m, n] = line.split(' ').map(Number); if (m > 9 || n > 9) { output = -1; } //注意break的使用,否则因为几个case并不是非此即彼的关系,而是同等并列的关系,如没有break //则在同一次循环中,程序会继续往下走; break; case 2 : let arr2 = line.split(' ').map(Number); if (arr2[0] > m-1 || arr2[2] > m-1 || arr2[1] > n-1 && arr2[3] > n-1) { output = -1; } break; case 3 : let x1 = parseInt(line); if (m + 1 > 9 || x1 > m - 1) { output = -1; } break; case 4 : let y1 = parseInt(line); if (n + 1 > 9 || y1 > n - 1) { output = -1; } break; case 0 : let [x2, y2] = line.split(' ').map(Number); if (x2 > m - 1 || y2 > n - 1) { output = -1; } break; } console.log(output); }