Writing FizzBuzz

后端 未结 30 2139
庸人自扰
庸人自扰 2020-12-04 08:50

Reading the coding horror, I just came across the FizzBuzz another time.

The original post is here: Coding Horror: Why Can\'t Programmers.. Program?

For thos

30条回答
  •  青春惊慌失措
    2020-12-04 09:28

    The FizzBuzz question is a great interview question. We have started using it in our interview process. It is astounding how many people cannot solve such a simple problem.

    Keep in mind, the original blog post was eventually locked due to a flood of people posting more solutions. Hahaha.

    Regardless, here is mine in C++! ^_^

    #include 
    using namespace std;
    
    int main(int argc, char** argv)
    {
        for (int i = 1; i <= 100; ++i)
        {
            bool isMultipleOfThree = (i % 3) == 0;
            bool isMultipleOfFive = (i % 5) == 0;
    
            if (isMultipleOfThree) cout << "Fizz";
            if (isMultipleOfFive) cout << "Buzz";
            if (!isMultipleOfThree && !isMultipleOfFive) cout << i;
    
            cout << '\n';
        }
    
        return 0;
    }
    

提交回复
热议问题