简体   繁体   English

CS50马里奥更多:想知道代码是否有效?

[英]CS50 Mario more: Want to know if the code is efficient?

I started the CS50 course recently, and I wanted to know if there ways to optimize the code or if it was the most efficient way to do at this stage.我最近开始了 CS50 课程,我想知道是否有优化代码的方法,或者它是否是现阶段最有效的方法。

void mariomore(int);
int main(void)
{
    int h;
    do
    {
        h = get_int("Height: ");
    }
    while (h <= 0 || h >= 9); //limiting the height of the pyramid
    mariomore(h);
}

void mariomore(int h)
{
    for (int i = 0; i < h; i++) // This for is to print the # rows
    {
        for (int k = h - 1; k > i; k--) //This for is to print the spaces
        {
            printf(" ");
        }
        for (int j = 0; j < i; j++) //This for is to print the # columns
        {
            printf("#");
        }
        printf("#"); 
        printf("  "); //to print space. to valid check with $$
        for (int m = 0; m < i; m++) //to print second half of pyramid
        {
            printf("#");
        }
        printf("#\n");
    }
}

I made very few changes.我做了很少的改动。 You can actually remove extra printf("#");您实际上可以删除额外的printf("#"); lines and place them in for loops by simply running loops with an additional instance.行并通过简单地运行带有附加实例的循环将它们放入 for 循环中。

Nevertheless, your code looks decent.不过,您的代码看起来不错。 You may add warnings when the user input for height is not between 1-9.当用户输入的高度不在1-9 之间时,您可以添加警告。 I skipped this part as this is not what you are interested in, my guess.我跳过了这部分,因为这不是你感兴趣的,我猜。

void mariomore(int);
int main(void)
{
    int h;
    do
    {
        h = get_int("Height: ");
    }
    while (h <= 0 || h >= 9); //limiting the height of the pyramid
    mariomore(h);
}

void mariomore(int h)
{
    for (int i = 0; i < h; i++) // This for is to print the # rows
    {
        for (int k = h - 1; k > i; k--) //This for is to print the spaces
        {
            printf(" ");
        }
        for (int j = 0; j < i + 1; j++) //This for is to print the # columns
        {
            printf("#");
        }
        printf("  "); //to print space. to valid check with $$
        for (int m = 0; m < i + 1; m++) //to print second half of pyramid
        {
            printf("#");
        }
        printf("\n");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM