用 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