Heim > Backend-Entwicklung > C++ > Hauptteil

Fizz-Buzz

DDD
Freigeben: 2024-09-18 13:28:02
Original
856 Leute haben es durchsucht

Fizz-Buzz

The programmer's Stairway to Heaven, there is no escaping Fizz Buzz. Cropping up in interviews everywhere, it's also a useful little task to write when learning a new language. This has the added benefit of potentially changing how you look at the problem.

My usual C# solution is very straightforward.

public static string FizzBuzz(int num)
{
    var divBy3 = num % 3 == 0;
    var divBy5 = num % 5 == 0;
    string word;

    if (divBy3 && divBy5)
    {
         word = "FizzBuzz";
    }
    else if (divBy3)
    {
        word = "Fizz";
    }
    else if (divBy5)
    {
        word = "Buzz";
    }
    else
    {
        word = num.ToString();
    }

    return word;
}

public static void Main(string[] args)
{
    for(var i=1; i<=100; i++)
    {
        var word = FizzBuzz(i);
        Console.Write($"{word} ");
    }
}
Nach dem Login kopieren

It wasn't until I wrote this in C that I realised how wasteful this approach is. Memory management in C is handled manually, so it was immediately obvious what the issue was. In the C# code we create a string for each call to FizzBuzz. This happens in a loop, so we rapidly allocate several strings. However each one is only used once in the loop. After that we have to wait for the garbage collector to notice this and reclaim the memory used by the string.

In C I decided to pass a buffer into the function instead. Each time through the loop we clear the buffer, write to it, then print it out. There is more work expected of the caller, but we are much more effecient in our usage of memory.

void fizzBuzz(int num, char *buf)
{
    bool isFizz = num % 3 == 0;
    bool isBuzz = num % 5 == 0;

    if (isFizz && isBuzz)
    {
        sprintf(buf, "%s", "FizzBuzz");
    }
    else if(isFizz)
    {
        sprintf(buf, "%s", "Fizz");
    }
    else if(isBuzz)
    {
        sprintf(buf, "%s", "Buzz");
    }
    else
    {
        sprintf(buf, "%d", num);
    }
}

int main()
{
    const size_t BUF_MAX = 128;
    char *buf = (char *)malloc(sizeof(char) * BUF_MAX);

    for (int i=1; i<100; i++)
    {
        memset(buf, '\0', BUF_MAX);
        fizzBuzz(i, buf);
        printf("%s ", buf);
    }

    free(buf);
    buf = NULL;

    return 0;
}
Nach dem Login kopieren

Taking time to write even simple things in other languages can change our perspective.

Das obige ist der detaillierte Inhalt vonFizz-Buzz. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:dev.to
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!