Fizz Buzz是一项非常简单的编程任务,在软件开发人员的工作面试中要求进行。
Fizz Buzz的典型回合可以是:
编写一个程序,打印一个从1到100的数字,并以’3’的倍数打印“ Fizz”,而不是以“ 5”的倍数打印“ Buzz”。
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14,
Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26,
Fizz, 28, 29, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz, ...
C++
// CPP program to print Fizz Buzz
#include
int main(void)
{
int i;
for (i=1; i<=100; i++)
{
// number divisible by 3 and 5 will
// always be divisible by 15, print
// 'FizzBuzz' in place of the number
if (i%15 == 0)
printf ("FizzBuzz\t");
// number divisible by 3? print 'Fizz'
// in place of the number
else if ((i%3) == 0)
printf("Fizz\t");
// number divisible by 5, print 'Buzz'
// in place of the number
else if ((i%5) == 0)
printf("Buzz\t");
else // print the number
printf("%d\t", i);
}
return 0;
}
C++ Using STL
// CPP program to print Fizz Buzz
#include
#include
#include
int main()
{
// dynamic array(range) of size 100 of int type
std :: vector range(100);
// 'iota' to fill the vector in increasing manner
std :: iota(range.begin(), range.end(), 1);
// initializing dynamic array of string type
std :: vector values;
// resize the vector(values) as that of vector(range)
values.resize(range.size());
//'auto' to deduce the type of the variable
auto fizzbuzz = [](int i) -> std::string
{
// number divisible by 15(will also be
// divisible by both 3 and 5), print 'FizzBuzz'
if ((i%15) == 0)
return "FizzBuzz";
// number divisible by 5, print 'Buzz'
if ((i%5) == 0)
return "Buzz";
// number divisible by 3, print 'Fizz'
if ((i%3) == 0)
return "Fizz";
// to print other numbers
return std::to_string(i);
};
// Operation to each of the elements in the
// range [begin(), end()) and stores the
// value returned by each operation in the
// range that begins at values.begin().
std :: transform(range.begin(), range.end(),
values.begin(), fizzbuzz);
for (auto& str: values)
std::cout << str << std::endl;
return 0;
}
Java
// Java program to print Fizz Buzz
import java.util.*;
class FizzBuzz
{
public static void main(String args[])
{
int n = 100;
// loop for 100 times
for (int i=1; i<=n; i++)
{
//number divisible by 15(divisible by
// both 3 & 5), print 'FizzBuzz' in
// place of the number
if (i%15==0)
System.out.print("FizzBuzz"+" ");
// number divisible by 5, print 'Buzz'
// in place of the number
else if (i%5==0)
System.out.print("Buzz"+" ");
// number divisible by 3, print 'Fizz'
// in place of the number
else if (i%3==0)
System.out.print("Fizz"+" ");
else // print the numbers
System.out.print(i+" ");
}
}
}
Python
# Python program to print Fizz Buzz
# Loop for 100 times i.e. range
for fizzbuzz in range(100):
# Number divisible by 15,(divisible
# by both 3 & 5), print 'FizzBuzz'
# in place of the number
if fizzbuzz % 15 == 0:
print("FizzBuzz")
continue
# Number divisible by 3, print 'Fizz'
# in place of the number
elif fizzbuzz % 3 == 0:
print("Fizz")
continue
# Number divisible by 5,
# print 'Buzz' in
# place of the number
elif fizzbuzz % 5 == 0:
print("Buzz")
continue
# Print numbers
print(fizzbuzz)
C#
// C# program to print Fizz Buzz
using System;
class GFG{
// Driver Code
public static void Main()
{
int n = 100;
// Loop for 100 times
for(int i = 1; i <= n; i++)
{
// Number divisible by 15(
// divisible by both 3 & 5),
// print 'FizzBuzz' in place
// of the number
if (i % 15 == 0)
Console.Write("FizzBuzz" + " ");
// Number divisible by 3,
// print 'Fizz' in place
// of the number
else if (i % 3 == 0)
Console.Write("Fizz" + " ");
// Number divisible by
// 5, print 'Buzz'
// in place of the number
else if (i % 5 == 0)
Console.Write("Buzz" + " ");
// Print the numbers
else
Console.Write(i + " ");
}
}
}
// This code is contributed
// by shiv_bhakt.
PHP
输出 :
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11
Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz
Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz
31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41
Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz
52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61
62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71
Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz
82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91
92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
Fizz Buzz的变体尝试
- 替换包含数字3或5的数字:代替替换因子3或5的数字,可以通过将包含数字3或5的数字替换为“嘶嘶声”或“嗡嗡声”来玩游戏。
例如:1、2,嘶嘶声,4,嗡嗡声,6、7、8、9、10、11、12,嘶嘶声,14,嗡嗡声,16、17、18、19、20、21、22,嘶嘶声,24 ,Buzz,26、27、28、29,Fizz,Fizz,Fizz,Fizz,Fizz,Fizz Buzz,Fizz,Fizz,Fizz,Fizz,40、41、42,Fizz,44,Buzz等…… - 仅替换数字中的数字(3或5):在此变体中,仅替换实际数字,因此23变为“二十嘶嘶”,而50变为“嗡嗡声”。可以将此变化与原始变化结合起来,以形成更具挑战性的序列。
例如:1、2,嘶嘶声,4,嗡嗡声,嘶嘶声,7、8,嘶嘶声,嗡嗡声,11,嘶嘶声,嘶嘶声,14,嘶嘶声,16、17,嘶嘶声,19,嗡嗡声,嘶嘶声,22,嘶嘶声,嘶嘶声,嗡嗡声,26,嘶嘶声,28、29,嘶嘶声,嗡嗡声,嘶嘶声,嘶嘶声,嘶嘶声,嘶嘶声,嘶嘶声,嘶嘶声,嘶嘶声,嘶嘶声,嘶嘶声,嘶嘶声,嗡嗡声,41,嘶嘶声,嘶嘶声,44,嘶嘶声,… - 更改基数:可以用10以外的数学基数进行游戏。例如,以5为基数进行游戏的过程如下:
例如:1、2,嘶嘶声,4,嗡嗡声,嘶嘶声,12、13,嘶嘶声,嗡嗡声,21,嘶嘶声,23、24,嘶嘶声,31、32,嘶嘶声,34,嗡嗡声,嘶嘶声,… - Fizz或Buzz,但不是Fizz-Buzz:更具挑战性的变化是Fizz或Buzz的游戏方向发生了变化,但Fizz-Buzz却没有。对于某些序列,这会使动作在2个或3个玩家之间反弹,并在爆发时导致误操作。例如,游戏的3/7版本具有12到18之间的复杂序列。
- Fizz Buzz Woof:一种变体已经扩展到拥有自己的相关名称的程度。在这种情况下,数字3变为Fizz,数字5变为Buzz,数字7变为Woof。该游戏的主要规则是,将包含该数字或可被该数字整除的任何数字替换为出现的单词。如果该数字具有该数字的2个实例(即33或55)并且可以被该数字整除,则在此示例中,该单词被说三遍。附加规则是必须按标题中给定的顺序说出单词(如果出现多个)。
例如:1,2,Fizz Fizz(3),4,Buzz Buzz(5),Fizz(6),Woof Woof(7),8,Fizz(9),Buzz(10),11,Fizz(12) ,Fizz(13),Woof(14),Fizz Buzz Buzz(15),16,Woof(17),Fizz(18),19,Buzz(20),Fizz Woof(21),22,Fizz(23), Fizz(24),Buzz Buzz(25),26,Fizz Woof(27),Woof(28),29,Fizz Fizz Buzz(30),Fizz(31),Fizz(32),Fizz Fizz Fizz(33), …
参考 :
https://zh.wikipedia.org/wiki/Fizz_buzz