如何用 5 种编程语言完成 FizzBu​​zz 挑战

FizzBu​​zz 挑战是一个经典的挑战,用作计算机程序员的面试筛选工具。这是一项非常简单的编程任务,但用于确定求职者是否真的会编写代码。

听起来有趣和令人兴奋?让我们开始吧。在本文中,您将学习如何通过 5 种编程语言的实现来解决 FizzBu​​zz 挑战。

问题陈述

您需要编写一个程序来打印从 1 到 100 的数字,以便:

  1. 如果数字是 3 的倍数,则需要打印“Fizz”而不是该数字。
  2. 如果数字是 5 的倍数,则需要打印“Buzz”而不是该数字。
  3. 如果数字是 3 和 5 的倍数,则需要打印“FizzBu​​zz”而不是该数字。

在转向解决方案之前,尝试在循环和条件语句的帮助下想出一个解决方案来解决这个挑战。

解决 FizzBu​​zz 挑战的方法

您需要按照以下方法来解决此挑战:

  1. 运行从 1 到 100 的循环。
  2. 能被 3 和 5 整除的数总是能被 15 整除。因此检查一个数是否能被 15 整除。如果这个数能被 15 整除,打印“FizzBu​​zz”。
  3. 检查一个数是否能被 3 整除的条件。如果这个数能被 3 整除,打印“Fizz”。
  4. 检查一个数是否能被 5 整除的条件。如果这个数能被 5 整除,打印“Buzz”。

注意:您可以使用模运算符 (%) 检查一个数字是否可以被另一个数字整除。例如:25 % 5 == 0,因此 25 可以被 5 整除。

FizzBu​​zz 挑战的伪代码

以下是 FizzBu​​zz 挑战的伪代码:

 for number from 1 to 100:
if (number is divisible by 3 and 5) then:
print("FizzBuzz")
if (number is divisible by 3) then:
print("Fizz")
if (number is divisible by 5) then:
print("Buzz")

相关:什么是编码以及它是如何工作的?

解决 FizzBu​​zz 挑战的 C++ 程序

下面是解决 FizzBu​​zz 挑战的 C++ 程序:

 // C++ program to implement the FizzBuzz problem
#include <iostream>
using namespace std;
int main()
{
for (int i=1; i<=100; i++)
{
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, "FizzBuzz" is printed in place of that number
if (i%15 == 0)
{
cout << "FizzBuzz" << " ";
}
// "Fizz" is printed in place of numbers
// that are divisible by 3
else if ((i%3) == 0)
{
cout << "Fizz" << " ";
}
// "Buzz" is printed in place of numbers
// that are divisible by 5
else if ((i%5) == 0)
{
cout << "Buzz" << " ";
}
// If none of the above conditions are satisfied,
// the number is printed
else
{
cout << i << " ";
}
}
return 0;
}

输出:

 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

相关:如何学习 C++ 编程:入门的最佳网站

解决 FizzBu​​zz 挑战的 Python 程序

下面是解决 FizzBu​​zz 挑战的 Python 程序:

 # Python program to implement the FizzBuzz problem
for i in range(1, 101):
# Numbers that are divisible by 3 and 5
# are always divisible by 15
# Therefore, "FizzBuzz" is printed in place of that number
if (i%15 == 0):
print("FizzBuzz", end=" ")
# "Fizz" is printed in place of numbers
# that are divisible by 3
elif (i%3 == 0):
print("Fizz", end=" ")
# "Buzz" is printed in place of numbers
# that are divisible by 5
elif(i%5 == 0):
print("Buzz", end=" ")
# If none of the above conditions are satisfied,
# the number is printed
else:
print(i, end=" ")

输出:

 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

相关:如何使用“Hello World”脚本开始使用 Python

解决 FizzBu​​zz 挑战的 JavaScript 程序

下面是解决 FizzBu​​zz 挑战的 JavaScript 程序:

 // JavaScript program to implement the FizzBuzz problem
for (let i=1; i<=100; i++) {
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, "FizzBuzz" is printed in place of that number
if (i%15 == 0) {
document.write("FizzBuzz" + " ");
}
// "Fizz" is printed in place of numbers
// that are divisible by 3
else if ((i%3) == 0) {
document.write("Fizz" + " ");
}
// "Buzz" is printed in place of numbers
// that are divisible by 5
else if ((i%5) == 0) {
document.write("Buzz" + " ");
}
// If none of the above conditions are satisfied,
// the number is printed
else {
document.write(i + " ");
}
}

输出:

 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

相关:新程序员的最佳初学者项目

解决 FizzBu​​zz 挑战的 Java 程序

下面是解决 FizzBu​​zz 挑战的 Java 程序:

 // Java program to implement the FizzBuzz problem
public class Main
{
public static void main(String args[])
{
for (int i=1; i<=100; i++)
{
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, "FizzBuzz" is printed in place of that number
if (i%15==0)
{
System.out.print("FizzBuzz"+" ");
}
// "Fizz" is printed in place of numbers
// that are divisible by 3
else if (i%3==0)
{
System.out.print("Fizz"+" ");
}
// "Buzz" is printed in place of numbers
// that are divisible by 5
else if (i%5==0)
{
System.out.print("Buzz"+" ");
}
// If none of the above conditions are satisfied,
// the number is printed
else
{
System.out.print(i+" ");
}
}
}
}

输出:

 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

解决 FizzBu​​zz 挑战的 C 程序

下面是解决 FizzBu​​zz 挑战的 C 程序:

 // C program to implement the FizzBuzz problem
#include <stdio.h>
int main()
{
for (int i=1; i<=100; i++)
{
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, "FizzBuzz" is printed in place of that number
if (i%15 == 0)
{
printf("FizzBuzz ");
}
// "Fizz" is printed in place of numbers
// that are divisible by 3
else if ((i%3) == 0)
{
printf("Fizz ");
}
// "Buzz" is printed in place of numbers
// that are divisible by 5
else if ((i%5) == 0)
{
printf("Buzz ");
}
// If none of the above conditions are satisfied,
// the number is printed
else
{
printf("%d ", i);
}
}
return 0;
}

输出:

 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

以“Hello, World!”开始您的编码之旅程序

“你好,世界!”程序是程序员熟悉新编程语言的第一步。它被认为是几乎所有语言中最简单的程序之一。

如果您是编程世界的新手并正在探索不同的语言,那么“Hello, World!”程序是开始使用新编程语言的最佳选择。