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 相当)

解いてみました。

解答コード例(昇順)

昇順に並べ替えて、末尾が最大値、先頭が最小値です。

JavaScript
const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");
//数列の長さを表す整数 n 
const n = Number(lines[0]);
//数列 a 昇順でソート
const a = lines[1].split(" ").map(Number).sort((a, b) => a - b);
//a の最大値、 a の最小値の順で、半角スペース区切りで出力
console.log(a[n - 1], a[0]);

解答コード例(降順)

降順に並べ替えて、先頭が最大値、末尾が最小値です。

JavaScript
const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");
//数列の長さを表す整数 n 
const n = Number(lines[0]);
//数列 a 降順でソート
const a = lines[1].split(" ").map(Number).sort((a, b) => b - a);
//a の最大値、 a の最小値の順で、半角スペース区切りで出力
console.log(a[0], a[n - 1]);
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?