Skip to content

FizzBuzz

English

FizzBuzz

The classic. Walk the numbers 1 to n and, for each, decide what string to add to a list:

  • divisible by both 3 and 5 → FizzBuzz
  • divisible by 3 → Fizz
  • divisible by 5 → Buzz
  • otherwise → the number itself, as a string

Check the "both" case first, or a multiple of 15 will be caught by the 3 test alone.

中文

FizzBuzz

经典题目。遍历数字 1 到 n,为每个数字决定往列表里加入哪个字符串:

  • 同时能被 3 和 5 整除 → FizzBuzz
  • 能被 3 整除 → Fizz
  • 能被 5 整除 → Buzz
  • 其他 → 数字本身的字符串形式

要先检查「两者都能」的情况,否则 15 的倍数会被单独的「能被 3 整除」判断先捕获。

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩