FizzBuzz Question

Using FizzBuzz to Find Developers who Grok Coding.

來,這是題目。不難理解的英文題目。

Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".


程式其實很容易就可以答出來了,就一些小地方要十分小心留意囉,即 3 和 5 的倍數要如何處理?!抓到要訣就答對了,呵呵。

@這裡有幾個參考
Imran On Tech
Coding Horror
PIL 的討論(連組語都出現了,太屌啦。)


'use aspx with vb.net
Private Function FizzBuzz() As String
  '偷懶的變數命名,好孩子不要學哦
  Dim i As Integer
  Dim out As String = ""

  For i = 1 To 100
    If i Mod 15 = 0 Then '3 和 5 的倍數要記得先處理掉,不然就破功了
      out += "FizzBuzz" & "<br />"
    ElseIf i Mod 3 = 0 Then
      out += "Fizz" & "<br />"
    ElseIf i Mod 5 = 0 Then
      out += "Buzz" & "<br />"
    'ElseIf i Mod 15 = 0 Then '這二行是錯的。一開始直覺是寫在這裡,執行後才發覺不對
    'out += "FizzBuzz" & "<br />"
    Else
      out += i.ToString & "<br />"
    End If
  Next

  Return out
End Function

0 留言