0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paizaラーニング レベルアップ問題集 線形探索メニュー応用編 JavaScript 三次元データ

Posted at

三次元データ (paizaランク B 相当)

解答例

三次元データですが、求めたい答えは二次元配列で求まります。
メソッドを駆使して解いてみました。

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");
//奥行きn、幅m、高さh,色x
const [n, m, h, x] = lines[0].split(" ").map(Number);
console.log(
  lines.slice(1)
    .map(line => line.split(" ").filter(v => Number(v) === x).length)
    .reduce((a, b) => a + b)
);

少しずつconsole.logしながら書いていくとわかりやすいと思います。

補足
//入力linesの2行目以降をslice(1)で配列にする
console.log(
  lines.slice(1)
);
//linesの要素1行1行をlineとしてsplitする
console.log(
  lines.slice(1)
    .map(line => line.split(" "))
);
//lineの要素を数値にして、xだけ残したいのでfilterする
console.log(
  lines.slice(1)
    .map(line => line.split(" ").filter(v => Number(v) === x)
);
//残ったxがいくつあるかlengthで求める
console.log(
  lines.slice(1)
    .map(line => line.split(" ").filter(v => Number(v) === x).length)
);
//「x何個あるか」の配列になったので、reduceで合計する
console.log(
  lines.slice(1)
    .map(line => line.split(" ").filter(v => Number(v) === x).length)
    .reduce((a, b) => a + b)
);
//以上で完成
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?