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

问题描述 投票:0回答:20

请帮助我使用右对齐的哈希值和空格正确打印高度为“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
20个回答
5
投票

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

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

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

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

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

0
投票

在你的主循环中你有:

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

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


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

0
投票

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

如果我们必须将数字 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++;
        }
    }     
}       
}

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


0
投票

这就是我如何让它为马里奥更舒适地工作。

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

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

}

0
投票

我认为您的代码在最后一个原始数据中将无法正常工作,因为第二个循环从为每个 i 迭代自动添加一个空块开始,但是构建基本原始数据的最后一个 i 迭代没有任何空块,但只有#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");
}
}

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

}

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

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

}

0
投票

在敲了我的头一段时间后,我发现了这个,并且它有效。

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

0
投票

我使用 do-while 循环和 for 循环的组合解决了这个问题。一旦我有了金字塔的第一部分;我添加了另一个 for 循环来创建金字塔的第二部分。

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

int main(void)
{
    int num_blocks;
    int hashes;
    int spaces;
    do
    {
        //prompt user for input
        num_blocks = get_int("Enter the number of blocks between (1 -8) inclusive: ");

        //check user input
        if (num_blocks > 8 || num_blocks <= 0)
        {
            num_blocks = get_int("Enter the number of blocks between (1 -8) inclusive: ");
        }
    }
    //build mario game
    while (num_blocks < 0);

    for (int i = 0; i < num_blocks; i++)
    {
        // print out spaces
        for (spaces = (num_blocks - i); spaces >= 2; spaces--)
        {
            printf(" ");
        }
        //loop to print out hashes
        for (hashes = 0; hashes <= i; hashes++)
        {
            printf("#");
        }
        printf("  ");
        //loop to print other hashes
        for (hashes = 0; hashes <= i; hashes++)
        {
            printf("#");
        }
        printf("\n");
    }
}

0
投票
#include <stdio.h>
#include <cs50.h>

void print_row(int length);
void print_space(int space);

int main()
{
    int column = get_int("What height pyramid do you want to make? : ");
    for (int i = 0; i < column; i++)
    {
        print_space(column - i - 1);  // Adjusted the space calculation
        print_row(i + 1);
    }
}

void print_row(int height)
{
    for (int j = 0; j < height; j++)
    {
        printf("#");
    }
    printf("\n");
}

void print_space(int space)
{
    for (int k = 0; k < space; k++)
    {
        printf(" ");
    }
}

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

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

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.