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?

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

Last updated at Posted at 2025-11-12

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


問題概要

前回は 一直線のテーブル(1番とn番はつながっていない)だったが、今回は 円卓(1番とn番がつながっている)。

つまり、
「最後の席(n)まで行ったら、次は1番に戻る」 というルール。


🔹入力

n
a b
  • n:座席の総数(円卓)
  • a:グループの人数
  • b:最初に座る席の番号

🔹出力

s_1 s_2 ... s_a

b番から a人分、席番号を順番に出力(※nの次は1に戻る)


入力例:

5
3 4

出力例:

4 5 1






✅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); // 人数aと開始位置bを取得
    
    const ans = []; // 座る席を入れる配列

    // bから順にa人分の席番号を決める
    for (let i = b; i < a + b; i++) {
        if (i <= n) {
            ans.push(i);      // n以内ならそのまま
        } else {
            ans.push(i - n);  // nを超えたら1番から再開
        }
    }






📝まとめ

  • 円卓(ループ構造)
    → 最後(n)の次は1番に戻る。

  • i - n の考え方
    → 「nを超えたら、nを引く」とループできる。
    例:n=5, i=66 - 5 = 1


  • mod(剰余)を使えばもっと簡潔に書ける!
const ans = [...Array(a)].map((_, i) => (b + i - 1) % n + 1);

🔸(b + i - 1):0始まりに合わせる
🔸% n:nを超えたら1に戻る
🔸+1:再び1始まりに戻す

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?