Writing FizzBuzz

后端 未结 30 1914
庸人自扰
庸人自扰 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:11

    I would suggest this compact code as an addition to the previous simple and nice versions.

    for (int i = 1; i <= 100; i++) // i++ but not ++i as in your example, be careful here
    {
        bool fizz = i % 3 == 0;
        bool buzz = i % 5 == 0;
        string output = fizz && buzz ? "FizzBuzz" :
                                fizz ? "Fizz" :
                                buzz ? "Buzz" :
                                i.ToString();
    
        Console.WriteLn(output);
    }
    
    0 讨论(0)
  • 2020-12-04 09:12

    I think you started with a complicated way. Improving that code would be more complicated. You can use a temp variable to track and display that variable at the end of the FizzBuzz check. Below is code and you can also watch this detail c# FizzBuzz youtube video ( http://www.youtube.com/watch?v=OX5TM3q-JQg ) which explains how the below code is implemented.

        for (int j = 1; j <= 100; j++)
        {
        string Output = "";
    
        if (j % 3 == 0) Output = "Fizz";// Divisible by 3 --> Fizz
    
        if (j % 5 == 0) Output += "Buzz"; // Divisible by 5 --> Buzz
    
        if (Output == "") Output = j.ToString(); // If none then --> number
    
        Console.WriteLine(Output); // Finally print the complete output
        }
    
    0 讨论(0)
  • 2020-12-04 09:12

    Not the most efficient, but here's one using C#-6 string interpolation:

    void Main()
    {
        for (int i = 1; i <= 100; i++)
        {
           Console.WriteLine($"{(i % 15 == 0 ? "FizzBuzz" : 
                                 i % 3 == 0 ? "Fizz" : 
                                 i % 5 == 0 ? "Buzz" : i.ToString())}");
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:13

    Will add my 5 cents to solution by Linq. Everybody is using Select, which is basically Map function. IMHO foldl function suits better to solve this quiz:

    Console.WriteLine(
                    Enumerable
                    .Range(1, 100)
                    .Aggregate(new StringBuilder(), (builder, i)
                        => i % 15 == 0 ? builder.AppendLine("FizzBuzz")
                         : i % 3 == 0 ? builder.AppendLine("Fizz")
                         : i % 5 == 0 ? builder.AppendLine("Buzz")
                         : builder.AppendLine(i.ToString()))
                    .ToString());
    
    0 讨论(0)
  • 2020-12-04 09:14

    You can use either use this and only take the amount you want

    static void Main(string[] args)
    {
        GetFizzBuzz().Take(100).ToList().ForEach(Console.WriteLine);
    }
    
    private static IEnumerable<string> GetFizzBuzz()
    {
        for (var i = 0; i < int.MaxValue; i++)
        {
            if (i % 3 == 0 && i % 5 == 0) yield return "FizzBuzz";
            if (i % 3 == 0) yield return "Fizz";
            yield return i % 5 == 0 ? "Buzz" : i.ToString(CultureInfo.InvariantCulture);
        }
    }
    

    Or simply use this :

    Enumerable.Range(1, 100).Select(s => {
        if (s % 3 == 0 && s % 5 == 0) return "FizzBuzz";
        if (s % 3 == 0) return "Fizz";
        return s%5 == 0 ? "Buzz" : s.ToString(CultureInfo.InvariantCulture);
    }).ToList().ForEach(Console.WriteLine);
    
    0 讨论(0)
  • 2020-12-04 09:16

    Unrolled for maximum efficiency. This program can outfizzbuzz all others.

    public void FizzBuzz()
    {
        const string FIZZ = "Fizz";
        const string BUZZ = "Buzz";
        const string FIZZBUZZ = "FizzBuzz";
    
        int i = 0;
        while (i < 150)
        {
            Console.WriteLine(++i);
            Console.WriteLine(++i);
            Console.WriteLine(FIZZ); ++i;
            Console.WriteLine(++i);
            Console.WriteLine(BUZZ); ++i;
            Console.WriteLine(FIZZ); ++i;
            Console.WriteLine(++i);
            Console.WriteLine(++i);
            Console.WriteLine(FIZZ); ++i;
            Console.WriteLine(BUZZ); ++i;
            Console.WriteLine(++i);
            Console.WriteLine(FIZZ); ++i;
            Console.WriteLine(++i);
            Console.WriteLine(++i);
            Console.WriteLine(FIZZBUZZ); ++i;
        }
    }
    
    0 讨论(0)
提交回复
热议问题