给定一个正整数N,则任务是从1打印所有的号码为N的“嘶嘶声”,“嗡嗡”和“嘶嘶声嗡嗡”分别替换的3,5和两个3和5的倍数。
例子:
Input: N = 5
Output: 1, 2, Fizz, 4, Buzz
Input: N = 15
Output: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14
Fizz Buzz
使用模运算符的方法:解决此问题的最简单方法是使用模运算符。有关此方法,请参考本文的上一篇文章。
不使用模运算运算符做法:将以上问题可以在不使用模运算符,因为需要解决:
- 模运算符是非常昂贵的操作。在最低级别上,模是除法。
- 编译器的DIV指令给出除法和取模结果。但是,现代CPU将专用的除法电路与查找表配合使用,因此,通过使用移位,速度不会有任何重大变化。
- 但是对于大整数,可以观察到,模运算程序的运行时比其他程序慢得多,运算复杂度为O(N 2 ) 。
请按照以下步骤解决问题:
- 初始化两个计数变量,例如count3和count5 ,以存储分别被3和5整除的数字的计数。
- 使用变量i遍历[1,N]范围,并执行以下步骤:
- 将count3和count5递增1 。
- 如果count3的值等于3 ,则打印“ Fizz”并设置count3 = 0 。
- 同样,如果count5的值等于5 ,则打印“ Buzz”并设置count5 = 0 。
- 如果以上条件都不符合,则打印i 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to generate FizzBuzz sequence
void fizzBuzz(int N)
{
// Stores count of multiples
// of 3 and 5 respectively
int count3 = 0;
int count5 = 0;
// Iterate from 1 to N
for (int i = 1; i <= N; i++) {
// Increment count3 by 1
count3++;
// Increment count5 by 1
count5++;
// Initialize a boolean variable
// to check if none of the
// condition matches
bool flag = false;
// Check if the value of count3
// is equal to 3
if (count3 == 3) {
cout << "Fizz";
// Reset count3 to 0, and
// set flag as True
count3 = 0;
flag = true;
}
// Check if the value of count5
// is equal to 5
if (count5 == 5) {
cout << "Buzz";
// Reset count5 to 0, and
// set flag as True
count5 = 0;
flag = true;
}
// If none of the condition matches
if (!flag) {
cout << i;
}
cout << " ";
}
}
// Driver Code
int main()
{
int N = 15;
fizzBuzz(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to generate FizzBuzz sequence
static void fizzBuzz(int N)
{
// Stores count of multiples
// of 3 and 5 respectively
int count3 = 0;
int count5 = 0;
// Iterate from 1 to N
for (int i = 1; i <= N; i++) {
// Increment count3 by 1
count3++;
// Increment count5 by 1
count5++;
// Initialize a boolean variable
// to check if none of the
// condition matches
boolean flag = false;
// Check if the value of count3
// is equal to 3
if (count3 == 3) {
System.out.print("Fizz");
// Reset count3 to 0, and
// set flag as True
count3 = 0;
flag = true;
}
// Check if the value of count5
// is equal to 5
if (count5 == 5) {
System.out.print("Buzz");
// Reset count5 to 0, and
// set flag as True
count5 = 0;
flag = true;
}
// If none of the condition matches
if (!flag) {
System.out.print(i);
}
System.out.print(" ");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 15;
fizzBuzz(N);
}
}
// This code is contributed by susmitakundugoaldanga.
Python3
# Python3 program for the above approach
# Function to generate FizzBuzz sequence
def fizzBuzz(N):
# Stores count of multiples
# of 3 and 5 respectively
count3 = 0
count5 = 0
# Iterate from 1 to N
for i in range(1, N + 1):
# Increment count3 by 1
count3 += 1
# Increment count5 by 1
count5 += 1
# Initialize a boolean variable
# to check if none of the
# condition matches
flag = False
# Check if the value of count3
# is equal to 3
if (count3 == 3):
print ("Fizz", end = "")
# Reset count3 to 0, and
# set flag as True
count3 = 0
flag = True
# Check if the value of count5
# is equal to 5
if (count5 == 5):
print ("Buzz", end = "")
# Reset count5 to 0, and
# set flag as True
count5 = 0
flag = True
# If none of the condition matches
if (not flag):
print (i, end = "")
print(end = " ")
# Driver Code
if __name__ == '__main__':
N = 15
fizzBuzz(N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to generate FizzBuzz sequence
static void fizzBuzz(int N)
{
// Stores count of multiples
// of 3 and 5 respectively
int count3 = 0;
int count5 = 0;
// Iterate from 1 to N
for(int i = 1; i <= N; i++)
{
// Increment count3 by 1
count3++;
// Increment count5 by 1
count5++;
// Initialize a bool variable
// to check if none of the
// condition matches
bool flag = false;
// Check if the value of count3
// is equal to 3
if (count3 == 3)
{
Console.Write("Fizz");
// Reset count3 to 0, and
// set flag as True
count3 = 0;
flag = true;
}
// Check if the value of count5
// is equal to 5
if (count5 == 5)
{
Console.Write("Buzz");
// Reset count5 to 0, and
// set flag as True
count5 = 0;
flag = true;
}
// If none of the condition matches
if (!flag)
{
Console.Write(i);
}
Console.Write(" ");
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 15;
fizzBuzz(N);
}
}
// This code is contributed by shikhasingrajput
输出:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
时间复杂度: O(N)
辅助空间: O(1)