简体   繁体   English

Cs50 PSET 1 马里奥不太舒服

[英]Cs50 PSET 1 Mario less comfortable

hey thanks for the help: screenshot嘿,谢谢你的帮助:截图

New to programming and trying to understand the logic why the code in line 17 produces the desire result of a left-aligned pyramid.编程新手并试图理解为什么第 17 行中的代码会产生左对齐金字塔的期望结果的逻辑。 Just trying to fully understand why x= height - y - 1 gives the desired outcome.只是试图完全理解为什么 x= height - y - 1 会给出预期的结果。

This snippet这个片段

for (int x = height - y - 1; x < height; x++)
{
    print("#");
}

does not have anything to do with alignment.与 alignment 没有任何关系。 The pyramid will always be left aligned without a space( " " ) printing loop, which you don't have in your code and you probably don't need either.金字塔将始终保持对齐,没有空格( " " )打印循环,您的代码中没有,您可能也不需要。

Now let's walk through the iteration to understand what's going on现在让我们通过迭代来了解发生了什么

Consider the height to be 5考虑height为5

In the first iteration of the outer loop (ie for (int y = 0; y < height; y++) )在外循环的第一次迭代中(即for (int y = 0; y < height; y++)

  • y = 0 y = 0
  • x = height - y - 1 = 5 - 0 - 1 = 4 x = 高度 - y - 1 = 5 - 0 - 1 = 4

So x starts at 4 and stops before reaching height , ie 5. So this loop will be executed exactly 1 time .所以x从 4 开始并在达到height之前停止,即 5。所以这个循环将被执行1 次 Which means it'll print a singular # .这意味着它将打印一个单数#

In the second iteration of the outer loop在外循环的第二次迭代中

  • y = 1 y = 1
  • x = height - y - 1 = 5 - 1 - 1 = 3 x = 高度 - y - 1 = 5 - 1 - 1 = 3

So x starts at 3 and stops before reaching height , ie 5. So this loop will be executed exactly 2 times .所以x从 3 开始并在达到height之前停止,即 5。所以这个循环将被执行2 次 Which means it'll print # twice .这意味着它会打印#两次

In the third iteration of the outer loop在外循环的第三次迭代中

  • y = 2 y = 2
  • x = height - y - 1 = 5 - 2 - 1 = 2 x = 高度 - y - 1 = 5 - 2 - 1 = 2

So x starts at 2 and stops before reaching height , ie 5. So this loop will be executed exactly 3 times .所以x从 2 开始并在达到height之前停止,即 5。所以这个循环将被执行3 次 Which means it'll print # thrice .这意味着它将打印#三次

In the fourth iteration of the outer loop在外循环的第四次迭代中

  • y = 3 y = 3
  • x = height - y - 1 = 5 - 3 - 1 = 1 x = 高度 - y - 1 = 5 - 3 - 1 = 1

So x starts at 1 and stops before reaching height , ie 5. So this loop will be executed exactly 4 times .所以x从 1 开始并在达到height之前停止,即 5。所以这个循环将被执行4 次 Which means it'll print # 4 times.这意味着它将打印# 4 次。

In the fifth and final iteration of the outer loop在外循环的第五次也是最后一次迭代中

  • y = 4 y = 4
  • x = height - y - 1 = 5 - 4 - 1 = 0 x = 高度 - y - 1 = 5 - 4 - 1 = 0

So x starts at 0 and stops before reaching height , ie 5. So this loop will be executed exactly 5 times .所以x从 0 开始并在达到height之前停止,即 5。所以这个循环将被执行5 次 Which means it'll print # 5 times .这意味着它将打印# 5 次

So to achieve that logic, is the reasoning behind using x = height - y - 1 .因此,要实现该逻辑,是使用x = height - y - 1背后的原因。 However there are other ways to do this too-然而,还有其他方法可以做到这一点——

for (int x = 0; x < y + 1; x++)
{
    print("#");
}

This will also work in the same logic, but hopefully with less confusion.这也将在相同的逻辑下工作,但希望减少混乱。

Notice, how the number of characters printed in each line matches with the line number .注意,每行打印的字符数如何与行号匹配。 So the first line has 1 hash, 2nd has 2 and so on.所以第一行有 1 个 hash,第二行有 2 个,依此类推。 We can deduce the line number from y .我们可以从y推导出行号。 For the first line y = 0 , for the second line y = 1 and so on.对于第一行y = 0 ,对于第二行y = 1 ,依此类推。 So we can simply add 1 to y and set that as our upper bound to print the hashes.所以我们可以简单地将1添加到y并将其设置为打印哈希值的上限。

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

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