如何使这个代码形成的金字塔(CS50 Mario Program)正确对齐?

时间:2015-08-31 02:30:23

标签: c stdio cs50

请帮我创建高度为“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;
}

15 个答案:

答案 0 :(得分:1)

在你的主循环内你有:

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

因此,每次循环时,它都正确地绘制了哈希值和新线,但是你不会告诉它每次绘制超过1个空格,这与哈希的子循环不同,指令并没有每次通过都会增加。

答案 1 :(得分:1)

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;
}

答案 2 :(得分:1)

#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");
    }
}

答案 3 :(得分:0)

#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");
    }
}

答案 4 :(得分:0)

#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");
}
}

答案 5 :(得分:0)

这是我从两方面解决此问题的方式,但是您可以只使用一个,只需使用2个for循环而不是3个即可。

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

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

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

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

在height变量旁边,我们需要另一个可以作为计数器的变量,因为我们无法操纵我们的height变量。

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

因此,最终的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++;
        }
    }     
}       
}

我实际上对此做了blog post,因为我喜欢记笔记,以防您需要更多信息。

答案 6 :(得分:0)

这就是我如何更轻松地为mario工作的方式。

#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");
    }
}

答案 7 :(得分:0)

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

#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");
    }
} 

答案 8 :(得分:0)

我认为您的代码在上一个原始文件中无法正常工作,因为第二个循环从为每个i-iteration自动添加一个空块开始,但是构建基础原始文件的最后一个i-iteration却没有有任何空块,但只有#s。 代码看起来应该像这样:

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

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

答案 9 :(得分:0)

    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
        // Get positive integer from user
        int height;
        do
        {
            height = get_int("Height: ");
        }
        while (height < 1 || height > 8);
    
        //for loop for next line
        for (int hash = 1; hash < height + 1; hash++)
        {
            for (int space = height - hash; space > 0; space--) // for loop to add spaces
            {
                printf(" ");
            }
            for (int hashwidth = 0; hashwidth < hash; hashwidth++)// for loop to add hashes
            {
                printf("#");
            }
            printf("\n");
        }

}

答案 10 :(得分:0)

可以使用 Stephen Prata 的 C Primer Plus 一书中所谓的 width.c 程序解决上述问题吗?

/* width.c -- field widths */
#include <stdio.h>
#define PAGES 959
int main(void)
{
printf("*%d*\n", PAGES);
printf("*%2d*\n", PAGES);
printf("*%10d*\n", PAGES);
printf("*%-10d*\n", PAGES);
return 0;
}

答案 11 :(得分:-1)

#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;
}

答案 12 :(得分:-1)

 //this is how you only make the right aligned pyramid
#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("#");
      }
    }
    return 0;
  }
  while(ht > 0 || ht < 23);


  return 0;
}

答案 13 :(得分:-1)

//Author: Andriyevski Serhiy
#include <stdio.h>
#include <cs50.h>

int main(void)
{

    // declaring my variables
    int height;
    int all_row;
    int space;
    int hash;

    // prompts user for integer until integer is 0 to 23
    do
    {
        printf("Please choose a number from 0 to 23:");
        height = GetInt();
    }
    while ((height < 0) || (height > 23));

    // this is the loop that will create the number of rows in the pyramid entered by the user
    for (all_row = 1; all_row <= height; all_row++) 
    {
        for (space = (height - all_row); space > 0; space--)
        {
            printf(" "); 
        }

        for (hash = 1; hash <= (all_row + 1); hash++)
        {   
            printf("#"); 
        }

        printf("\n");
    }
    return 0;
}

答案 14 :(得分:-2)

int main(void)
{
    do
    {
        height = get_int("Height: "); //Requesting input from user for height.
    }

    while (height < 0 || height > 8); //Limits user input to between 0 and 8.

    for (int rows = 1; rows <= height; rows++)
    {
        for (int spaces = 0; spaces < (height - rows); spaces++) //Based on the row number, there will be a certain amount of spaces. E.g. If the height is 8 and it is row 1, there are 7 spaces.
        {
            printf(" ");
        }

        for (int hashes = 0; hashes < rows; hashes++) //Based on which row it is, the number of hashes printed is equal to the row number. E.g. row 1 has 1 hash.
        {
            printf("#");
        }

        printf("  ");

        for (int hashes = 0; hashes < rows; hashes++) //This prints the right side of the pyramid. No need to print the spaces.
        {
            printf("#");
        }
        printf("\n");
    }

希望这段代码足够合理,可以阅读并希望它有所帮助!

此外,它帮助我意识到终端从上到下打印,这是为这个问题设计算法的一个主要因素。