制作哈希金字塔 [英] Making a Hash Pyramid

查看:84
本文介绍了制作哈希金字塔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前正在参加CS-50课程,想知道是否有人可以帮助我。我应该创建一个程序,要求用户输入1-23之间的高度(并不断提示用户,直到给出有效答案为止)---我能够对该部分进行编码。

Currently doing the CS-50 course and was wondering if anyone could help me with this. I'm supposed to create a program which will ask a user for a height between 1-23 (and continuously prompt the user until a valid answer is given) --- I was able to code that part.

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

int main(void)
{ 
    int height;
    do
    {
        printf("please give me a height between 1-23: ");
        height = GetInt();
    }    
    while (height < 1 || height > 23);
}

do while循环似乎可以达到预期目的。现在,给定变量 height的程序现在需要创建该高度的金字塔。金字塔的底部与终端的左下角对齐,并且其最后的行以2个散列结束,例如:

The do while loop seems to do what its intended. Now, the program, given the variable "height" now needs to create a pyramid of that height. The bottom of the pyramid is to be aligned with the bottom left hand of the terminal and its last "row" is to finish with 2 hashes as such:

高度4:

    ##
   ###
  ####
 #####

但是对于金字塔1-23的任何高度,代码都必须是通用的。
这是我很难过的地方(实际上是在编写代码来绘制它)。

But the code needs to be generic for any height of pyramid 1-23. This is where I'm having a hard time (in actually making a code to draw this).

我已经注意到,对于每一行,需要的散列值(如果我们将第一行称为行1,然后将其称为行2,以此类推...则是
行号+1
如果需要的话,可以用行高数字表示。
如果有人能够向我解释我如何使用C编写此程序,将不胜感激!:)

Ive noticed that for each row, the number of hashes needed (if we call the top row "row 1" and the subsequent "row 2" and so on... is row number+1. As for the amount of spaces that are needed, can be represented by height-row number. If someone would be able to explain to me how I could write this program using C, it would be much appreciated! :)

推荐答案

这里是实现此目的的一种方法。基本上,您需要从下至上构建金字塔。一旦您看到了循环结构,该任务就很容易了,要想准确地打印出正确数量的空格和哈希符号就很难了:

Here is a way you can implement this. Basically, you need to build the pyramid from the bottom up. The task is easy once you see the loop structure, its just tricky to get the math down for printing the correct number of spaces and hash symbols:

#include <stdio.h>

int main(void)
{ 
    int height, i, j;
    do
    {
        printf("please give me a height between 1-23: ");
        height = GetInt();
    }    
    while (height < 1 || height > 23);

    printf("\n");    
    for (i = 0; i < height; i++) {

        for (j = 0; j < height - i - 1; j++)
            printf(" ");
        for (j = 0; j < i + 2; j++)
            printf("#");

        printf("\n");
    }
}

要详细了解发生的事情以及每个原因循环是必需的:

For more clarification on whats going on, and why each loop is necessary:


  1. 循环外部:变量 i 对应到金字塔的一排。 i 的值将在后两个循环中保持不变

  1. Outer for loop: the variable i corresponds to a row in the pyramid. the value of i will remain constant for each of the second two loops

第一个内部for循环:对于任何行,都必须有 height-i-2 个空格。您可以找出原因,因为总行宽将为 height ,并且任何行都具有 i + 2 哈希符号,因此必须有高度-(i + 2)=高度-i-1 个空格。因此,基本上,此循环仅打印所需的空格。您可以使用变量 j

First inner for loop: for any row, there needs to be height - i - 2 spaces. You can figure this out because the total row width will be height, and any row has i + 2 hash symbols, so there needs to be height - (i + 2) = height - i - 1 spaces. So basically, this loop just prints the required spaces. You can track this with the variable j

第二个for循环进行跟踪:此循环类似于第一个内部循环,但是您现在需要打印井号。在循环开始时,您重置 j 并计数到所需的哈希标记数

Second inner for loop: This loop is similar to the first inner loop, but you need to now print the hash marks. At the beginning of the loop you reset j and count up to the required number of hash marks

这篇关于制作哈希金字塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆