FizzBuzz various

If you don't understand what this guy is saying, skip it

First C

Of course, it is often easier to draw using C ++-specific grammar, but I write with a grammar that can be written in C as well.

#include <stdio.h>

void loop(int n);
void fizzbuzz(int x);

#define N 100

#define TWO(i) fizzbuzz(i); fizzbuzz(i + 1);
#define FOUR(i) TWO(i) TWO(i + 2)
#define EIGHT(i) FOUR(i) FOUR(i + 4)
#define SIXTEEN(i) EIGHT(i) EIGHT(i + 8)
#define THIRTY_TWO(i) SIXTEEN(i) SIXTEEN(i + 16)
#define SIXTY_FOUR(i) THIRTY_TWO(i) THIRTY_TWO(i + 32)
#define HUNDRED(i) SIXTY_FOUR(i) THIRTY_TWO(i + 64) FOUR(i + 96)

int main(){
    int array[100];

    //FizzBuzz with a for statement normally
    for(int i = 0; i < 100; i++)
        fizzbuzz(i);
    printf("\n");

    //FizzBuzz with pointer subtraction
    for(int *i = &array[0]; i != &array[N]; i++)
        fizzbuzz((int)(i - &array[0]) + 1);

    //macro
    HUNDRED(1)

    //Recursion
    loop(100);
    printf("\n");

    //while statement
    int j = 0;
    while(++j < 100)
        fizzbuzz(j);
    printf("\n");

    return 0;
}

void fizzbuzz(int x){
    if(x % 15 == 0){
        printf("fizzbuzz");
    }else if(x % 3 == 0){
        printf("fizz");
    }else if(x % 5 == 0){
        printf("buzz");
    }else{
        printf("%d", x);
    }

    printf(" ");
}

void loop(int n){
    if(n <= 0)
        return;
    
    loop(n - 1);
    fizzbuzz(n);
}

Using the subtraction of the second pointer, FizzBuzz takes advantage of the fact that the array is a continuous data structure. So you can't do the same with a linked list. FizzBuzz, which uses the third macro, had a bit of a mess to define the macro. If you usually use rep macros, you can see what you are doing. FizzBuzz uses the 4th recursion, but in theory all loops that can be written with for statements and while statements can be written with recursion, so it is inevitably possible to write with recursion. However, in reality, a loop using recursion is heavier than using for or while and consumes memory, so it is better to use that when writing with for or while. If you can write with for, you can also write with while. So you can write FizzBuzz with while. Well, in reality, it is easier to read if you write it in for, so many people use for.

Implemented in C ++

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <numeric>

using namespace std;
#define N 100

string fizzbuzz(int x);

int main(){
    vector<int> vec_int(N);
    
     // for_Implemented in each
    for(vector<int>::iterator i = vec_int.begin(); i < vec_int.end(); i++){
        *i = distance(vec_int.begin(), i) + 1;
    }
    for_each(vec_int.begin(), vec_int.end(), [](intelement){cout<<fizzbuzz(element)<<"";});
    cout << endl;

    //Implemented with accumulate
    fill(vec_int.begin(), vec_int.end(), 1);
    accumulate(vec_int.begin(), vec_int.end(), 0, [](intx,inty){cout<<fizzbuzz(x)<<"";returnx+y;});
    cout << endl;

    //Implemented with transform
    vector<string> output_string(N);
    for(vector<int>::iterator i = vec_int.begin(); i < vec_int.end(); i++){
        *i = distance(vec_int.begin(), i) + 1;
    }
    transform(vec_int.begin(), vec_int.end(), output_string.begin(), [](intinput){returnfizzbuzz(input);});
    for_each(output_string.begin(), output_string.end(), [](strings){cout<<s<<"";});

    return 0;
}
string fizzbuzz(int x){
    if(x % 15 == 0){
        return "fizzbuzz";
    }else if(x % 3 == 0){
        return "fizz";
    }else if(x % 5 == 0){
        return "buzz";
    }else{
        return to_string(x);
    }
}

A lot of loops can be rewritten using the algorithm package. Sometimes I do AC without using for or while in competitive programming. However, the reason why few people do it is that readability is reduced. If you write multiple processes in a lambda expression, the readability will drop at once. The second FizzBuzz is doing the operation of writing to the standard output while calculating the sum of the elements of the array with accumulate. Functions that use lambda expressions in the algorithm package can do something similar. It is a function called transform used in the third FizzBuzz, but in ruby and Lisp it is called a map function. Here, it is used to convert an int type vector to a string type vector. I will omit it here, but you can also implement it using list or set.

Implementation in Java

import java.util.ArrayList;
import java.util.List;

class Main {
    public static void main(String[] args){
        for(int i = 0; i < 100; i++)
            FizzBuzz(i);
        System.out.println();

        int n = 0;
        while(n++ < 100)
            FizzBuzz(n);
        System.out.println();

        Loop(100);
        System.out.println();

        int length = 100;
        List<Integer> numbers = new ArrayList<Integer>(length);
        for(int i = 0; i < length; i++){
            numbers.add(i);
        }

        numbers.stream()
            .forEach((i) -> { FizzBuzz(i); });
        System.out.println();

        numbers.stream()
            .map((i) -> { return toString(i); })
            .forEach((i) -> { System.out.print(i); });
        System.out.println();

        for(int i = 0; i < length; i++){
            numbers.set(i, 1);
        }
        numbers.stream()
            .reduce((i, j) -> { FizzBuzz(i); return i + j; });
        System.out.println();
    }

    static void FizzBuzz(int n){
        if(n % 15 == 0)
            System.out.print("FizzBuzz");
        else if(n % 3 == 0)
            System.out.print("Fizz");
        else if(n % 5 == 0)
            System.out.print("Buzz");
        else
            System.out.print(n);
        System.out.print(" ");
    }

    static void Loop(int n){
        if(n <= 0)
            return;
        Loop(n - 1);
        FizzBuzz(n);
    }
    static String toString(int n){
         if(n % 15 == 0)
            return "FizzBuzz ";
        else if(n % 3 == 0)
            return "Fizz ";
        else if(n % 5 == 0)
            return "Buzz ";
        else
            return Integer.toString(n) + " ";
    }
}

The first, second, and third are the for statement, while statement, and recursion implementation that were also implemented in C. It's implemented in the 4th ArrayList, but you should be able to implement it in the LinkedList as well. ~~ However, I used this because the Linked List was not very comfortable to use. What you have to be careful about with ~~ ArrayList is that if you do not specify the number of elements at the time of the first variable declaration, the array will be recreated every time you add an element, so it will take time for the length of the array. is not it. The 5th, 6th, and 7th functions are stream, which is the same as Linq in C #. It's different from Linq in that you can't use SQL-like syntax. I used this because Accumulate in C ++ was a function called reduce in Java. ~~ I love convolution functions. ~~ Similarly, the function called transform in C ++ was a function called map in Java. As you can see by comparison, it is easier to read using Java Stream than using the functions of the C ++ algorithm package.

Recommended Posts

FizzBuzz various
FizzBuzz problem
Various design patterns
Spring injection various
[Ruby] FizzBuzz problem
Various MVC models
Various GraphQL tests
I tried FizzBuzz.
FizzBuzz program (2 types)
FizzBuzz in Java