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ランク C 相当)

解答例(for,if)

forとifを使って普通に解きます。a[n-2]=a[n-1]まで調べます。

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");

const n = Number(lines[0]);
const a = lines[1].split(" ").map(Number);

let ans = 0;
for (let i = 0; i < n - 1; i++) {
  if (a[i] === a[i + 1]) {
    ans += 1;
  }
}
console.log(ans);

解答例(ワンライナー)

reduceを使って一行で解いてみました。

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");

console.log(lines[1].split(" ").map(Number).reduce((acc, cur, index, arr) => 
cur === arr[index + 1] ? acc += 1 : acc, 0));
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?