C语言 如何让这段代码形成的金字塔(CS50马里奥程序)右对齐?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32303244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:04:54  来源:igfitidea点击:

How to make the pyramid (CS50 Mario Program) formed by this code to be right aligned?

cstdiocs50

提问by SKammala

Please help me create the pyramid with height "n" print correctly using hashes and spaces that is right-aligned. I have posted the code itself below. The program correctly asks for the user input, but doesn't build the pyramid right-aligned. If anyone can fix this, please help.

请帮助我使用右对齐的哈希和空格正确创建高度为“n”的金字塔。我已经在下面发布了代码本身。该程序正确地要求用户输入,但不会构建右对齐的金字塔。如果有人可以解决这个问题,请帮忙。

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

int main(void)
{
    int i=0;
    int n=0;

    do
    {
        printf("Height:");
        n=GetInt();
    } while (n<0 || n>23);

    for (i=0; i<n; i++)
    {
        printf(" ");
        for (int x=0; x<i+2; x++)
        {
            printf("#");
        }
        printf("\n");
    }

    return 0;
}

回答by Palaniichuk Dmytro

int main(void){
    int height;
    int spaces;
    int dashes;

    do 
    {
        printf("Height:");
        height = GetInt();
    }
    while (height <= 0 || height >= 23);
    //loop for the rows
    for (int i = 1; i <= height; i++){ //fixed the <= part of the operator
        //loop for the spaces and dashes per row
        for (spaces = (height - i); spaces >= 0; spaces--){
            printf(" ");
        }
        for (dashes = 1; dashes <= (i + 1); dashes++){
            printf("#");    
        }
        printf("\n");
    }
    return 0;
}

回答by Michael Elimu

#include<cs50.h>
#include<stdio.h>
#include<stdio.h>
void half_pyramid(int n);

int main(void){
    int height;
    do{
        height = get_int("Height: ");
    }while(height<=0||height>23);
    half_pyramid(height);
}
void half_pyramid(int n)
   {
    int spaces;     
    int dashes;
    for(int i = 2; i<=n+1;i++){
        for(spaces = (n-i); spaces>=0;spaces--){
            printf(" ");
        }
        for (dashes = 1; dashes <= i; dashes++){
            printf("#");
        }
        printf("\n");
    }
}

回答by Amoury

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

int main(void)
{
  int height;  
  int spaces;
  int hash;

do
{
    height = get_int("Height: ");
}
while (height < 0 || height > 23);


for (int i = 0; i < height; i++)
{
    // For loop to print out the spaces
    for (spaces = (height - i); spaces >= 2; spaces--)
    {
        printf(" ");

    }
    // For loop to print out hashes
    for (hash = 0; hash <= (i + 1); hash++)
    {
        printf("#");
    }
    printf("\n");
}
}

回答by Goran Spasojevic

Here is how I solved the version of this problem with both sides, but you can make it work with only one, just use 2 for-loops instead of three.

这是我如何解决这个问题的双方版本,但您可以只使用一个,只需使用 2 个 for 循环而不是 3 个。

If we have to take a number 1 – 8 as an input which is the height of our pyramid, we can use one for loop to print out each row of the pyramid.

如果我们必须将数字 1 - 8 作为我们金字塔的高度作为输入,我们可以使用一个 for 循环来打印出金字塔的每一行。

Inside this loop, we will need another two for loops that print spaces and hashes.

在这个循环中,我们将需要另外两个用于打印空格和散列的 for 循环。

And since we need the other side of the pyramid as well, we will use three loops in total within our main loop.

由于我们也需要金字塔的另一边,我们将在主循环中总共使用三个循环。

Why three loops and not four? Because we don't actually need to print space on the right side of the pyramid.

为什么是三个循环而不是四个?因为我们实际上并不需要在金字塔的右侧打印空间。

Beside the height variable we need another one that will work as a counter, since we cannot manipulate our height variable.

除了高度变量之外,我们还需要另一个用作计数器的变量,因为我们无法操纵高度变量。

These for loops work in opposite direction, i.e. the more spaces we have, less hashes we print and vice versa.

这些 for 循环以相反的方向工作,即我们拥有的空间越多,我们打印的哈希就越少,反之亦然。

So, the final CS50 Pset 1 Mario More solution looks exactly like this:

因此,最终的 CS50 Pset 1 Mario More 解决方案如下所示:

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

int main(void)
{
int height;

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

if (height > 0 || height < 9) {
    int counter = 0;
    for (int row=0; row<height; row++) {
        if (counter != height) {
            for (int spaces = (height-1) - counter; spaces > 0; spaces--) {
                printf(" ");
            }
            for (int hashes = 0; hashes <= counter; hashes++) {
                printf("#");
            }

  //ignore this block below if you want only one side

            printf("  "); 

            for (int hashes = 0; hashes <= counter; hashes++) {
                printf("#");
            }

  //##################################################

            printf("\n");
            counter++;
        }
    }     
}       
}

I actually made a blog postabout this since I like to keep notes in case that you need more information.

我实际上写了一篇关于这个的博客文章,因为我喜欢做笔记,以防你需要更多信息。

回答by james

This is how I got it to work for mario more comfortable.

这就是我让它让马里奥更舒服地工作的方式。

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

int main(void)
{
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 1 || n > 25);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (i + j < n - 1)
                printf(" ");
            else
                printf("#");  
        }
            printf("  ");
            for (int j = 0; j < n; j++)
                {
                    if (n - i < j + 2)
                        printf("#");
                }       
                printf("\n");
    }
}

回答by Nand Kishor

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

int main()
{

    int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 1);

    for (int i = 0; i<n; i++)
  {
      for (int j = 0; j < i+1; j++)
      {
          printf("#");
      }
      printf("\n");
  }

}

回答by harshdeeply

Here's one of the correct solutions!

这是正确的解决方案之一!

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

void buildPyramid(int height);

int main(void)
{
    // Initialize the variable height
    int height;

    // Run the loop to get a value of height between 1 and 8, inclusive, from the user
    do
    {
        height = get_int("Height: ");
    }
    while (height < 1 || height > 8);

    // Call the function and pass height to it as a parameter
    buildPyramid(height);
}

// Declare the function buildPyramid
void buildPyramid(int height)
{
    // Loop to add a new line
    for (int i = 0; i < height; i++)
    {
        // Loop to add spaces
        for (int k = height - i; k > 1; k--)
        {
            printf(" ");
        }
        // Loop to add hashes
        for (int j = 0; j <= i; j++)
        {
            printf("#");
        }
        printf("\n");
    }
} 

回答by Philip Burgess

Inside your main loop you have:

在你的主循环中,你有:

// Instruction for printing 1 space
// Sub loop for printing x+1 number of hashes
// Instruction for printing a new line

So each time it loops round it's correctly drawing the hashes and a new line but your not telling it to draw any more than 1 space each time as unlike the subloop for the hashes the instruction doesn't increase with each pass.

因此,每次循环时,它都会正确绘制散列和新行,但您不会告诉它每次绘制超过 1 个空格,因为与散列的子循环不同,指令不会随着每次传递而增加。

回答by Vladimir

#include<stdio.h>
#include<cs50.h>
int main (void)
{
    int height;
    int c = 0;
    do 
    {
        printf("Height?\n");
        height = GetInt();                                   

    }while(height < 0 || height > 23); 

    for(int a = height; a > 0; a--)               
    {
        for(int b = 0; b < a - 1; b++)                  
        {
            printf(" ");

        }
        printf("#");
        c++;
        for (int d = 0; d < c; d++)
        {
            printf("#");
        }
        printf("\n");
    }
}

回答by user6358853

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


  int main(void){
  int s=0;
  int hash=0;
  int ht;
  int h;

  do{
    printf("Height: ");
     ht=GetInt();
     for (h = ht; h > 0 ; h--){
      printf("\n");
      hash = ht - (h - 1);
      s = ht - hash;
      int i;
      for (i = 0; i <= s; i++){
        printf(" ");
      }
      for (i = 0; i <= hash; i++){
        printf("#");
       }
      printf("  ");
      for (i = 0; i <= hash; i++){
        printf("#");
      }
    }
     return 0;
  }
  while(ht > 0 || ht < 23);


  return 0;
}