标签:return func 闭包 个数 block etl for 题目 grid
在这个 n×n 方阵中,4个在同一方向(从下至上、从上至下、从右至左、从左至右或者对角线)上相邻的数的乘积最大是多少?
把乘积先存在一个数组里,然后从成绩数组里找最大值。
const testGrid = [
[40, 17, 81, 18, 57],
[74, 4, 36, 16, 29],
[36, 42, 69, 73, 45],
[51, 54, 69, 16, 92],
[7, 97, 57, 32, 16]
];
function largestGridProduct(arr) {
let len = arr.length;
largest = arr[0][0]
//横向的4位数之和
console.log('横向的4位数之和')
for (let row = 0; row < len; row++) {
for (let col = 0; col < len - 3; col++) {
console.log(arr[row][col], arr[row][col + 1], arr[row][col + 2], arr[row][col + 3])
largest = getLargest(arr[row][col] * arr[row][col + 1] * arr[row][col + 2] * arr[row][col + 3])
}
}
//纵向的4位数之和
console.log('纵向的4位数之和')
for (let row = 0; row < len - 3; row++) {
for (let col = 0; col < len; col++) {
console.log(arr[row][col], arr[row + 1][col], arr[row + 2][col], arr[row + 3][col])
largest = getLargest(arr[row][col] * arr[row + 1][col] * arr[row + 2][col] * arr[row + 3][col])
}
}
console.log('\对角线4为数之和')
//\对角线4为数之和
for (let row = 0; row < len - 3; row++) {
for (let col = 0; col < len - 3; col++) {
console.log(arr[row][col], arr[row + 1][col + 1], arr[row + 2][col + 2], arr[row + 3][col + 3])
largest = getLargest(arr[row][col] * arr[row + 1][col + 1] * arr[row + 2][col + 2] * arr[row + 3][col + 3])
}
}
console.log('/对角线4为数之和')
///对角线4为数之和
for (let row = 3; row < len; row++) {
for (let col = 3; col < len; col++) {
console.log(arr[row][col], arr[row - 1][col - 1], arr[row - 2][col - 2], arr[row - 3][col - 3])
largest = getLargest(arr[row][col] * arr[row - 1][col - 1] * arr[row - 2][col - 2] * arr[row - 3][col - 3])
}
}
console.log(largest)
return largest;
}
function fun() {
let largest = 0;
return function (n) {
if (n > largest) {
largest = n;
}
return largest
}
}
let getLargest = fun();
largestGridProduct(testGrid);
用闭包,存最大值
const testGrid = [
[40, 17, 81, 18, 57],
[74, 4, 36, 16, 29],
[36, 42, 69, 73, 45],
[51, 54, 69, 16, 92],
[7, 97, 57, 32, 16]
];
function largestGridProduct(arr) {
let len = arr.length;
productArr = [];
//横向的4位数之和
console.log('横向的4位数之和')
for (let row = 0; row < len; row++) {
for (let col = 0; col < len - 3; col++) {
console.log(arr[row][col], arr[row][col + 1], arr[row][col + 2], arr[row][col + 3])
productArr.push(arr[row][col] * arr[row][col + 1] * arr[row][col + 2] * arr[row][col + 3])
}
}
//纵向的4位数之和
console.log('纵向的4位数之和')
for (let row = 0; row < len - 3; row++) {
for (let col = 0; col < len; col++) {
console.log(arr[row][col], arr[row + 1][col], arr[row + 2][col], arr[row + 3][col])
productArr.push(arr[row][col] * arr[row + 1][col] * arr[row + 2][col] * arr[row + 3][col])
}
}
console.log('\对角线4为数之和')
//\对角线4为数之和
for (let row = 0; row < len - 3; row++) {
for (let col = 0; col < len - 3; col++) {
console.log(arr[row][col], arr[row + 1][col + 1], arr[row + 2][col + 2], arr[row + 3][col + 3])
productArr.push(arr[row][col] * arr[row + 1][col + 1] * arr[row + 2][col + 2] * arr[row + 3][col + 3])
}
}
console.log('/对角线4为数之和')
///对角线4为数之和
for (let row = 3; row < len; row++) {
for (let col = 3; col < len; col++) {
console.log(arr[row][col], arr[row - 1][col - 1], arr[row - 2][col - 2], arr[row - 3][col - 3])
productArr.push(arr[row][col] * arr[row - 1][col - 1] * arr[row - 2][col - 2] * arr[row - 3][col - 3])
}
}
console.log(fun(productArr))
return fun(productArr);
}
function fun(arr) {
let n = 0;
for (i of arr) {
if (i > n) {
n = i
}
}
return n
}
largestGridProduct(testGrid);
标签:return func 闭包 个数 block etl for 题目 grid
原文地址:https://www.cnblogs.com/superlizhao/p/12207203.html