There is an English version (translated by ChatGPT o3) below Japanese version.

問題について

この問題は有名なプログラミング入門問題「FizzBuzz」を少し改題したものです。この問題が難しいと感じた方は、先にそちらの問題を解いてみるとよいかもしれません。

解法

この問題に解答するあたって、以下の知識が必要です。

  • 標準入力や標準出力
  • for 文などの繰り返し処理
  • if 文などの条件分岐処理
  • 剰余演算

これらを既知とした上で、本問題は以下のアルゴリズムで解くことができます。

  1. 入力を受け取る。
  2. i=1,2,Ni = 1, 2, \dots N について、繰り返し以下の処理を行う。
    1. AiA_i33 で割ったあまりが 00 かつ AiA_i55 で割ったあまりが 00 ならば、PhysicsBirthday を出力する。
    2. 上記の場合以外で、AiA_i33 で割ったあまりが 00 ならば、Physics を出力する。
    3. 上記の場合以外で、AiA_i55 で割ったあまりが 00 ならば、Birthday を出力する。
    4. 上記の場合以外ならば、AiA_i を出力する。

なお、2.1 の手順で、「AiA_i1515 で割ったあまりが 00 」という条件にしても同じです。 また、本問題の性質を観察すると、2.1 の条件分岐を省略することもできます(後述)。

実装(C++)

実装の際に気をつけるポイントなどを以下に示します。正しく実装したはずなのに、正解が得られない場合にも参考にしてください。

  • vector などの配列は、宣言時にサイズを確定させておく。特に、NN を入力から受け取る前に NN で初期化してしまうミスに注意。
  • vector などの配列は、0-indexedであることに注意。すなわち、AA11 番目の要素を取得する場合、A[0] とする。
  • forループ内の、初期化条件、終了条件、更新処理のミスに注意。特に、終了条件の不等号のミスや、更新処理のみ ij にするミス。
  • 条件式内の等号は、= ではなく、== であることに注意。
  • PhysicsBirthdayPhysicsBirthday の間に空白を含まないように注意。
  • 条件分岐は誤っていないか確認する。特に、else ifif に変えてしまうミスが多い。
  • 文字列は "" で囲む。 '' とは異なる記号であることに注意。

実装例を以下に示します。

実装(Python)

実装の際に気をつけるポイントなどを以下に示します。正しく実装したはずなのに、正解が得られない場合にも参考にしてください。

  • 入力は、int型にキャストする必要がある。しない場合、文字列として扱われてしまう。
  • for i in range(N): と書いた場合、i=0,1,N1i = 0, 1, \dots N-1 であることに注意。
  • list などの配列は、0-indexedであることに注意。すなわち、AA11 番目の要素を取得する場合、A[0] とする。
  • 条件式内の等号は、= ではなく、== であることに注意。
  • PhysicsBirthdayPhysicsBirthday の間に空白を含まないように注意。
  • 条件分岐は誤っていないか確認する。特に、elifif に変えてしまうミスが多い。

実装例を以下に示します。

条件分岐を1つ減らす実装

今回、「AiA_i33 で割り切れるかつ 55 で割り切れる」場合の出力 PhysicsBirthday は、 「AiA_i33 で割り切れる」場合の出力 Physics と、「AiA_i55 で割り切れる」場合の出力 Birthday を結合したものです。
したがって、「AiA_i33 で割り切れる」→「AiA_i55 で割り切れる」の順で独立で条件分岐をし、それぞれの分岐では改行をしないことで、 両条件がどちらも真である場合に、文字列を結合したかのような出力を得ることができます(実装例参照)。
以下に実装例を示します。

EnglishVersion

About the Problem

This task is a slight variant of the classic beginner programming exercise “FizzBuzz.”
If you find this problem difficult, you may want to solve the original FizzBuzz problem first.

Solution

To solve this problem you will need the following knowledge:

  • Standard input and output
  • Loops such as the for statement
  • Conditional branches such as the if statement
  • The modulo operator

Assuming you are familiar with these, the problem can be solved with the following algorithm:

  1. Read the input.
  2. For i=1,2,,Ni = 1, 2, \dots , N, repeat the following:
    1. If Aimod3=0A_i \bmod 3 = 0 and Aimod5=0A_i \bmod 5 = 0, output PhysicsBirthday.
    2. Otherwise, if Aimod3=0A_i \bmod 3 = 0, output Physics.
    3. Otherwise, if Aimod5=0A_i \bmod 5 = 0, output Birthday.
    4. Otherwise, output AiA_i.

In step 2.1 you may equivalently check whether Aimod15=0A_i \bmod 15 = 0.
After observing the structure of the problem, you can even remove that branch (see below).

Implementation (C++)

Below are some tips to keep in mind while coding. They can also help track down mistakes if your program compiles but does not produce the correct answer.

  • When you declare a vector, be sure its size is determined after you read NN. Do not mistakenly initialize it with NN before reading the value.
  • Remember that arrays such as vector are 0-indexed. The first element of A is accessed with A[0].
  • In a for loop, double-check the initialization, termination, and update parts. Off-by-one errors in the termination condition or updating the wrong variable (e.g., i vs. j) are common.
  • Use == for equality in conditions, not =.
  • Do not insert a space between Physics and Birthday when printing PhysicsBirthday.
  • Make sure your branching logic is correct—many errors come from writing if instead of else if.
  • Enclose string literals in double quotes "..."; they are different from single quotes '...'.

Example implementation:

Implementation (Python)

Hints for the Python version:

  • Cast the input to int; otherwise it is treated as a string.
  • for i in range(N): iterates with i=0,1,,N1i = 0, 1, …, N − 1.
  • Lists are 0-indexed; the first element of A is A[0].
  • Use == for equality in conditions, not =.
  • Do not insert a space between Physics and Birthday when printing PhysicsBirthday.
  • Check your branching logic—changing an elif to if by accident is a common bug.

Example implementation:

Reducing the Number of Branches

The output PhysicsBirthday for “divisible by both 3 and 5” is simply the concatenation of the outputs for “divisible by 3” (Physics) and “divisible by 5” (Birthday). Therefore, you can branch on “divisible by 3” first, then independently on “divisible by 5”, printing without a newline in each branch. If neither condition is true, print A_iA\_i. Finally, print a newline. Example: