Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Поиск подматрицы (https://javascript.ru/forum/misc/79837-poisk-podmatricy.html)

Nicanor13 31.03.2020 15:07

Поиск подматрицы
 
Матрицу A мы назавем "десятичной ", если после удаления каждой строки и столбца i, в полученной подматрице сумма элементов больше или равно 10 -и.

Например

A = [
      [2, 4, 6],
      [2, 2, 2], 
      [4, 8, 8]
]

Нужно написать функцию которая получает матрицу и возвращает true если она "десятичная " и false в обратном случае.

function matrixElementsSum(matrix) {}
console.log(decimalSubMatrix([[2, 4, 6],[2, 2, 2],[4, 8, 8]]))   //  true           
console.log(decimalSubMatrix([[1, 1, 1],[1, 1, 1],[1, 2, 1]]))    //  false               
console.log(decimalSubMatrix([[4, 6, 8, 1],[2, 3, 5, 6],[1, 2, 8, 1], [9, 1, 0, 3]]))  // true
console.log(decimalSubMatrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))  //  false


Поможете решить задачу?

рони 31.03.2020 16:11

Nicanor13,
const sumArr = a => a.reduce((a,b) => a + b, 0);
const matrixСolumns = a => {
const {length} = a;
const columns = [];
for (let i = 0; i < length; i++) {
let temp = columns[i] = [];
for (let j = 0; j < length; j++) {
   temp.push(a[j][i])
}
}
return columns;
}

function decimalSubMatrix(matrix)
{ let sumTotal = sumArr(matrix.map(sumArr));
  if(sumTotal < 20) return false;
  const max = sumTotal - 10;
  const arr = matrix.slice(0).concat(matrixСolumns(matrix));
  return arr.every(a => sumArr(a) <= max)
}

console.log(decimalSubMatrix([[5, 0, 5],[0, 0, 0],[5, 0, 5]]))   //  true
console.log(decimalSubMatrix([[2, 4, 6],[2, 2, 2],[4, 8, 8]]))   //  true
console.log(decimalSubMatrix([[1, 1, 1],[1, 1, 1],[1, 2, 1]]))    //  false
console.log(decimalSubMatrix([[4, 6, 8, 1],[2, 3, 5, 6],[1, 2, 8, 1], [9, 1, 0, 3]]))  // true
console.log(decimalSubMatrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))  //  false


Часовой пояс GMT +3, время: 09:24.