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?

FizzBuzz問題を解く(変則)

Last updated at Posted at 2025-05-13

FizzBuzz問題出ると、みんな「if文使ってあまり0なら~」って解き方ばっかりで、捻くれてる私はこうしてるっていう。
(学生の時、課題で出た時に、講師ですら理解してくれなくて悲しい思い出)

サクッと手元で組めるのがPowerShellだったので。

 $start = 1  #初期値
 $end   = 100

 $x03 = 3 #倍数3
 $x05 = 5 #倍数5

 for ($i = $start; $i -le $end; $i++){
    $result = 0             #結果を記録
    if($i -eq $x03){
        $result += 1
        $x03 += 3
    }
    if($i -eq $x05){
        $result += 2
        $x05 += 5
    }

    # 結果を表示
    switch ($result) {
        1 {write-host "Fizz"; break}
        2 {write-host "Buzz"; break}
        3 {write-host "Fizz Buzz"; break}
        default {write-host $i; break}
    }

 }

$resultは何してるの?」って人は2進に置き換えたら分かるかも。

  • 余りを求めない
  • if祭りにならないから分かりやすい(はず

処理的にもこっちのが負担少ないはず(しらんけど
もうちょっと弄ったら、対象の倍数が変わったりとか、増減したりとか、対応しやすいように書けるはず。

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?