【问题标题】:How to make the pyramid (CS50 Mario Program) formed by this code to be right aligned?如何使这段代码形成的金字塔(CS50 马里奥程序)右对齐?
【发布时间】:2015-08-31 02:30:23
【问题描述】:

请帮助我使用右对齐的散列和空格正确创建高度为“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;
}

【问题讨论】:

标签: c stdio cs50


【解决方案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");
    }
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      #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");
      }
      }
      

      【讨论】:

        【解决方案4】:
        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;
        }
        

        【讨论】:

        • 欢迎来到 Stack Overflow!虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
        • @JonathanLeffler 没有任何上下文,因为该解决方案可能github repository from 2014...复制和粘贴
        【解决方案5】:

        在你的主循环中,你有:

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

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

        【讨论】:

        • 这不是问题的解决方案;它类似于伪代码。
        【解决方案6】:
        #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");
            }
        }
        

        【讨论】:

          【解决方案7】:

          这是我如何解决这个问题的版本,但你可以让它只使用一个,只需使用 2 个 for 循环而不是三个。

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

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

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

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

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

          这些 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,因为我喜欢做笔记,以防您需要更多信息。

          【讨论】:

            【解决方案8】:

            这就是我让它更舒适地为马里奥工作的方法。

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

            【讨论】:

              【解决方案9】:
              #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");
                }
              
              }
              

              【讨论】:

              • 添加一些关于您为修复它所做的工作的解释会很有帮助。
              【解决方案10】:

              我认为您的代码在最后一个 raw 中无法正常工作,因为第二个循环首先为每个 i-iteration 自动添加一个空块,但是最后一个 i-iteration 构建基本 raw 不会有任何空块,但只有#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");
              }
              }
              

              【讨论】:

                【解决方案11】:
                    #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");
                        }
                
                }
                

                【讨论】:

                  【解决方案12】:

                  上述问题可以使用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;
                  }
                  

                  【讨论】:

                    【解决方案13】:
                    #include<cs50.h>
                    #include<stdio.h>
                    
                    int main()
                    {
                    
                        int n;
                        do
                        {
                            n = get_int("Height: ");
                        }
                        while (n < 1 || n > 8);
                    
                        for (int i = 0; i<n; i++)
                      {
                          for (int k=n-i; k>1 ; k--)
                          {
                              printf(" ");
                          }
                          for (int j = 0; j < i+1; j++)
                          {
                              printf("#");
                          }
                          printf("\n");
                      }
                    
                    }
                    

                    【讨论】:

                    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
                    【解决方案14】:

                    敲了一会儿头后,我遇到了这个,它有效。

                    #include <stdio.h>
                    #include <cs50.h>
                    
                    int main(void)
                    {
                        int height = 0;
                        int line = 0;
                        int column = 0;
                    
                        while (true)
                        {
                            height = get_int("Height: ");
                    
                            if (height >= 1 && height <= 8)
                            {
                                break;
                            }
                        }
                    
                        /* Chooses a row: */
                        for ( line = 1 ; line <= height ; line++ )
                        {
                            /* Chooses a column: */
                            for ( column = 1 ; column <= height ; column++ )
                            {
                                /* Prints an hash character or a space character: */
                                if ( column >= height + 1 - line )
                                {
                                    printf("#");
                                }
                                else
                                {
                                    printf(" ");
                                }
                            }
                    
                            printf("\n");
                        }
                    }
                    

                    【讨论】:

                      【解决方案15】:
                      #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;
                      }
                      

                      【讨论】:

                      • 欢迎来到 Stack Overflow!虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
                      • 不能解决问题。您忽略了在打印任何内容之前输入有效数字的要求。
                      【解决方案16】:
                       //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;
                      }
                      

                      【讨论】:

                      • 欢迎来到 Stack Overflow!虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
                      【解决方案17】:
                      //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;
                      }
                      

                      【讨论】:

                      • 你不应该只是粘贴一堆代码,你应该解释它
                      【解决方案18】:
                      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");
                          }
                      

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

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

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2016-12-22
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多