题解 | #矩阵乘法#
矩阵乘法
https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { // Write your code here while(line = await readline()){ const x = +line // 转化整数否则出错 const y = +await readline() const z = +await readline() const firstMatrix = [] const secondMatrix = [] for (let i = 0 ; i < x; i++){ let line = (await readline()).split(' ') firstMatrix.push(line) } for (let i = 0; i < y; i++){ let line = (await readline()).split(' ') secondMatrix.push(line) } // 二维数组,如果仅仅new Array(x)并不fill(0) map出错 const newArray = new Array(x).fill(0).map(_=>{ return new Array(z).fill(0) }) for (let i = 0; i < x; i++){ for (let j = 0; j < z; j++){ const row = firstMatrix[i] // 二维数组,如果仅仅new Array(x)并不fill(0) map出错 const col = new Array(y).fill(0).map((_, index)=>{ return secondMatrix[index][j] }) let temp = 0 for(let k = 0; k < y; k++){ temp += row[k]*col[k] } newArray[i][j] = temp } } // 输出结果 for (let i = 0; i < newArray.length; i++){ console.log(newArray[i].join(' ')) } } }()
死题目,注意二维数组创建的方式就好