課題:Mario More

課題は、マリオブラザーズでお馴染みのブロックを「#」を使って作るというもの。

https://cs50.harvard.edu/x/2021/psets/1/mario/more/

screenshot of Mario jumping over adjacent pyramids

回答例

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    // Promt user for input
    int n;

    do
    {
        n = get_int("Height: ");
    }
    while (n < 1 || n > 8);

    // Outer loop
    for (int i = 0; i < n; i++)
    {
        // Inner loop, print space
        for (int j = 0; j < n - i - 1; j++)
        {
            printf(" ");
        }

        // print uphill hashtag #
        for (int k = 0; k <= i; k++)
        {
            printf("#");
        }

        // gap in between
        printf("  ");

        // print downhill hashtag #
        for (int l = 0; l <= i; l++)
        {
            printf("#");
        }

        // New line
        printf("\n");
    }

}