用 C++ 实现 Fizz Buzz 解决方案

Jinku Hu 2023年1月30日 2021年6月28日
C++
  1. 使用带有文字值的迭代方法在 C++ 中实现 Fizz Buzz 解决方案
  2. 使用自定义类在 C++ 中实现 Fizz Buzz 解决方案
用 C++ 实现 Fizz Buzz 解决方案

本文将介绍如何用 C++ 实现 Fizz Buzz 的解决方案。

使用带有文字值的迭代方法在 C++ 中实现 Fizz Buzz 解决方案

Fizz Buzz 是一个微不足道的问题,有时用作培训网站甚至面试的编程练习。它基本上归结为将 1100 的数字打印到控制台,除了 35 的倍数应分别替换为 FizzBuzz 字符串。此外,还有一个 15 的倍数规则,应该打印为 FizzBuzz。该任务可以通过一个简单的循环语句来解决,该循环语句遍历 100 个整数范围,并在其中包含 4 种不同情况的 if 条件。请注意,要检查数字是否是给定整数的倍数,应使用模数运算符 - %

#include <iostream>

using std::cout; using std::endl;

constexpr int COUNT = 100;

int main(){

    for (int i = 1; i <= COUNT; ++i) {

        if (i % 3 == 0)
            cout << "Fizz, ";
        else if (i % 5 == 0)
            cout << "Buzz, ";
        else if (i % 15 == 0)
            cout << "FizzBuzz, ";
        else
            cout << i << ", ";

    }

    return EXIT_SUCCESS;
}

输出:

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz, 31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz, 43, 44, Fizz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, Fizz, 61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, Fizz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, Fizz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz,

使用自定义类在 C++ 中实现 Fizz Buzz 解决方案

或者,我们可以实现一个类来存储给定的整数和相应字符串的映射,然后调用内置函数将值打印到控制台。请注意,此方法更通用,可用于扩展问题定义。FizzBuzz 类有一个类型为 map<int, string> 的数据成员和一个引用 Map 的构造函数。checkFizzBuzz 不接受任何参数并执行两个嵌套循环来检查 1-100 范围内的每个数字。

#include <iostream>
#include <string>
#include <map>

using std::cout; using std::endl;
using std::string; using std::cin;
using std::map;

constexpr int COUNT = 100;

class FizzBuzz {
    map<int, string> table;
public:
    explicit FizzBuzz(map<int, string> &init):
            table(std::move(init)) {}

    void checkFizzBuzz() {
        for (int i = 1; i <= COUNT; ++i) {
            for (const auto &item : table) {
                i % item.first == 0 ?
                cout << item.second << ", " :
                cout << i << ", ";
                break;
            }
        }
    }
};

int main(){
    map<int, string> init = {{3, "Fizz"},
                            {5, "Buzz"},
                            {15, "FizzBuzz"}};

    FizzBuzz fii(init);
    fii.checkFizzBuzz();

    return EXIT_SUCCESS;
}

从前面的示例代码中重新设计 FizzBuzz 类的另一种方法是定义一个构造函数,该构造函数采用整数/字符串对的初始化列表,如 map 本身。课程的其余部分保持不变。还可以将 COUNT 数据成员添加到 FizzBuzz 类本身并使用构造函数对其进行初始化,以允许用户根据需要传递不同的数字。

#include <iostream>
#include <string>
#include <map>
#include <initializer_list>

using std::cout; using std::endl;
using std::string; using std::cin;
using std::map;

constexpr int COUNT = 100;

class FizzBuzz {
    map<int, string> table;
public:
    FizzBuzz(std::initializer_list<std::pair<int, string>> init) {
        for (const auto &item : init) {
            table.insert(item);
        }
    }

    void checkFizzBuzz() {
        for (int i = 1; i <= COUNT; ++i) {
            for (const auto &item : table) {
                i % item.first == 0 ?
                cout << item.second << ", " :
                cout << i << ", ";
                break;
            }
        }
    }
};

int main(){
    FizzBuzz fii = {{3, "Fizz"},
                    {5, "Buzz"},
                    {15, "FizzBuzz"}};

    fii.checkFizzBuzz();

    return EXIT_SUCCESS;
}
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn