1
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 区間のソート

Last updated at Posted at 2022-09-12

区間のソート (paizaランク C 相当)

sliceで区間を切り出します。

解答コード例

JavaScript
const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");
//数列の長さを表す整数 n 
//ソートする区間の左端の位置を表す l と右端の次の位置を表す r 
const [n, l, r] = lines[0].split(" ").map(Number);
//数列 a
const a = lines[1].split(" ").map(Number);
//数列 a の l 番目の要素から r - 1 番目の要素だけを昇順でソート
const aSorted = a.slice(l, r).sort((a, b) => a - b);
//操作後の数列 a を半角スペース区切りで出力
console.log(aSorted.join(" "));
1
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
1
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?