简体   繁体   English

我的 CS50 mario 不太舒服的代码有什么问题?

[英]What is wrong with my CS50 mario less comfortable code?

Here is a copy of my code:这是我的代码的副本:

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

int main(void)
{
    int height = get_int("Please enter a height between 1 and 8 (inclusive)\nHeight: ");

    if(0 < height && height < 9)
    for(int i = 0; i <= height; i++)
    {
        for(int s = (height - 1); s >= 1; s--)
        {
            printf("a");
        }

        for(int h = 1; h <= i; h++)
        {
            printf("#");
        }

        printf("\n");
    }

    else
    printf("Please try again.\n"),
    main();
}

The part I cannot seem to get right is printing the correct number of spaces as it does not seem to reduce the variable "s" after each loop.我似乎无法正确打印的部分是打印正确数量的空格,因为它似乎没有在每个循环之后减少变量“s”。 (I have replaced the spaces with the letter "a" so I can see where the code has gone wrong.) The aim of the code is to print a pyramid of #'s that is right-aligned. (我已经用字母“a”替换了空格,这样我就可以看到代码哪里出错了。)代码的目的是打印一个右对齐的#金字塔。 Where have I gone wrong here?我在这里哪里出错了?

So for example, if the user inputs a height of 3, the output will be:例如,如果用户输入高度 3,则 output 将是:

aa

aa#

aa##

aa###

however, I would like the output to be:但是,我希望 output 是:

aa#

a##

###

The main issue is the 2nd for loop in your code when you print "a".主要问题是打印“a”时代码中的第二个 for 循环。 Everytime you hit in this particular loop, you basically print (height - 1) times "a".每次你在这个特定的循环中,你基本上打印(高度 - 1)乘以“a”。 In fact, you should print every time one less "a" (and one more hashes).事实上,你应该每次少一个“a”(多一个哈希)就打印出来。 To achieve this, we need to tweak condition of s.为此,我们需要调整 s 的条件。 Instead of this而不是这个

int s = height - 1; s >= 1

Use this用这个

int s = height - i; s > 1

Since your main loop has iterator i , we can use it here.由于您的主循环有迭代器i ,我们可以在这里使用它。

Second issue is that according to CS50 problem set description, you should actually print " " (spaces), not "a" .第二个问题是,根据 CS50 问题集描述,您实际上应该打印" " (空格),而不是"a" For this, you should replace "a" with " " .为此,您应该将"a"替换为" "

Third issue is your code prints an extra line with full of space in the begining.第三个问题是您的代码在开头打印了一个充满空间的额外行。 You need to tweak the main for loop with this condition to remove one extra line.您需要使用此条件调整主 for 循环以删除额外的一行。

i < height

And in the for loop where you print # symbol, you need to start iterator h from zero, so that it prints right away with the first line.在打印#符号的 for 循环中,您需要从零开始迭代器 h,以便它立即打印第一行。

int h = 0

The better solution is to use the built-in functionality of printf that is already in place, reducing the problem to a single, simple loop.更好的解决方案是使用已经存在的printf的内置功能,将问题简化为一个简单的循环。

#include <stdio.h>

int main(void) {
    int height = get_int("Please enter a height between 1 and 8 (inclusive)\nHeight: ");

    unsigned char blocks[height];
    memset(blocks, '#', height);

    for(int i=0; i<height; ++i)
    {
        printf("%*.*s\n", height, i+1, blocks);
    }
    return 0;
}

Output Output

Success #stdin #stdout 0s 4212KB
    #
   ##
  ###
 ####
#####

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

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