There is an English version (translated by ChatGPT o3) below Japanese version.
この問題は有名なプログラミング入門問題「FizzBuzz」を少し改題したものです。この問題が難しいと感じた方は、先にそちらの問題を解いてみるとよいかもしれません。
この問題に解答するあたって、以下の知識が必要です。
これらを既知とした上で、本問題は以下のアルゴリズムで解くことができます。
PhysicsBirthday を出力する。Physics を出力する。Birthday を出力する。なお、2.1 の手順で、「 を で割ったあまりが 」という条件にしても同じです。 また、本問題の性質を観察すると、2.1 の条件分岐を省略することもできます(後述)。
実装の際に気をつけるポイントなどを以下に示します。正しく実装したはずなのに、正解が得られない場合にも参考にしてください。
vector などの配列は、宣言時にサイズを確定させておく。特に、 を入力から受け取る前に で初期化してしまうミスに注意。vector などの配列は、0-indexedであることに注意。すなわち、 の 番目の要素を取得する場合、A[0] とする。i を j にするミス。= ではなく、== であることに注意。PhysicsBirthday の Physics と Birthday の間に空白を含まないように注意。else if を if に変えてしまうミスが多い。"" で囲む。 '' とは異なる記号であることに注意。実装例を以下に示します。
実装の際に気をつけるポイントなどを以下に示します。正しく実装したはずなのに、正解が得られない場合にも参考にしてください。
int型にキャストする必要がある。しない場合、文字列として扱われてしまう。for i in range(N): と書いた場合、 であることに注意。list などの配列は、0-indexedであることに注意。すなわち、 の 番目の要素を取得する場合、A[0] とする。= ではなく、== であることに注意。PhysicsBirthday の Physics と Birthday の間に空白を含まないように注意。elif を if に変えてしまうミスが多い。実装例を以下に示します。
今回、「 が で割り切れるかつ で割り切れる」場合の出力 PhysicsBirthday は、
「 が で割り切れる」場合の出力 Physics と、「 が で割り切れる」場合の出力 Birthday を結合したものです。
したがって、「 が で割り切れる」→「 が で割り切れる」の順で独立で条件分岐をし、それぞれの分岐では改行をしないことで、
両条件がどちらも真である場合に、文字列を結合したかのような出力を得ることができます(実装例参照)。
以下に実装例を示します。
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.
To solve this problem you will need the following knowledge:
for statementif statementAssuming you are familiar with these, the problem can be solved with the following algorithm:
PhysicsBirthday.Physics.Birthday.In step 2.1 you may equivalently check whether .
After observing the structure of the problem, you can even remove that branch (see below).
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.
vector, be sure its size is determined after you read . Do not mistakenly initialize it with before reading the value.vector are 0-indexed. The first element of A is accessed with A[0].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.== for equality in conditions, not =.Physics and Birthday when printing PhysicsBirthday.if instead of else if."..."; they are different from single quotes '...'.Example implementation:
Hints for the Python version:
int; otherwise it is treated as a string.for i in range(N): iterates with .A is A[0].== for equality in conditions, not =.Physics and Birthday when printing PhysicsBirthday.elif to if by accident is a common bug.Example implementation:
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 . Finally, print a newline. Example: