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?

「長テーブルのうなぎ屋」を解くために:part3

Last updated at Posted at 2025-11-11

今回は paiza の「「長テーブルのうなぎ屋」を解くために:part3」の問題に挑戦!


問題概要

うなぎ屋のテーブルには n 個の座席 が一直線に並んでいる。
(※円ではなく、1番とn番はつながっていない)

あるグループが来店し、
「座席番号 b から a 人が順に座る」とき、
その人たちが どの座席に座るか を求めて出力する。

🔹入力

n
a b
  • n:座席の総数(1行目)
  • a:グループの人数(2行目)
  • b:最初に座る人の座席番号

🔹出力

s_1 s_2 ... s_a
  • グループ全員の座席番号を順に出力(スペース区切り)



入力例:

6
4 2

出力例:

2 3 4 5






✅OK例:

const rl = require('readline').createInterface({ input:process.stdin });

const lines = [];

rl.on('line', (input) => lines.push(input));

rl.on('close', () => {
    const n = Number(lines[0]);
    const [a, b] = lines[1].split(' ').map(Number);
    
    const ans = [];
    
    for (let i = b; i < a + b; i++) {
        ans.push(i);
    }
    
    console.log(ans.join(' '));
});

流れ:

  1. 1行目から座席数 n を読む(今回は使わない)
  2. 2行目から人数 a と最初の席 b を読む
  3. 空の配列 ans を作る
  4. b から a 人分の席番号を順に ans に入れる
  5. 配列をスペース区切りで出力




🔍短くしてみた

const [n, a, b] = [Number(lines[0]), ...lines[1].split(' ').map(Number)];
console.log([...Array(a)].map((_, i) => b + i).join(' '));

流れ:

  1. 1行目と2行目を同時に分解して n, a, b を取得
  2. 長さ a の空配列を作る(Array(a))
  3. 各要素のインデックス i を使って、b + i の座席番号を作る
  4. 配列をスペース区切りで出力






📝まとめ

b から a 人、順に番号を作るだけ」
for文でも map でも、やってることは同じ!

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?