倒金字塔

时间:2015-11-28 18:02:13

标签: c

我正在尝试使用数字做倒数三角形。我认为代码的数字部分是正确的,但间距不起作用。

#include <stdio.h>
//Declare function
int triangle(int n);

//Main
int main(void){
  int height;
  do{
    printf("height:");
    scanf("%d", &height);
  }while (height < 1 && height > 9);
  triangle(height);}

//Function
int triangle(int n)
{
  int x,j,linhas, spaces;

//Number of lines
  for(linhas = 0; linhas < n; linhas++){

//Print spaces NOT FUC****** WORKING!!!
    for(spaces =0; spaces < (linhas + 1); spaces ++){
      printf(" ");}
//Fill in the numbers
    do{
    //Increasing part till n
      for (x = 0; x < n; x++){
        printf(" %d ", (x+1));}
//Decreasin part from n
      for (j = 0; j < (n-1) ; j++){
        printf(" %d ", ((n-1) -j));}
//New line after each line
      printf("\n");
      n--;
    }while(n > 0);}}

输出是:

 height:5
      1  2  3  4  5  4  3  2  1 
     1  2  3  4  3  2  1 
     1  2  3  2  1 
     1  2  1 
     1 

任何人都可以帮助输出的间距:

 height:5
      1  2  3  4  5  4  3  2  1 
         1  2  3  4  3  2  1 
            1  2  3  2  1 
               1  2  1 
                  1 

4 个答案:

答案 0 :(得分:2)

所以,问题在于循环。你减少计数(变量n),保持循环直到n> 0并且'空格'做一次。试着这样做。

int triangle(int n)
{
    int x,j,linhas, spaces, m;
    //Number of lines
    m=n;
    for(linhas = 0; linhas < n; linhas++){
        for(spaces =0; spaces < linhas; spaces ++){
            printf("   ");
        }
        //do{
            for (x = 0; x < m; x++){
                printf(" %d ", (x+1));
            }
            for (j = 0; j < (m-1) ; j++){
                printf(" %d ", ((m-1) -j));
            }
            printf("\n");
            m--;
        //}while(n > 0);
    }
}

答案 1 :(得分:1)

你的代码搞砸了。它应该有2个嵌套循环,如下所示:

while (n > 0)
{
    for (...) // print decreasing
    {
    }
    for (...) // print increasing
    {
    }
}

然后在第一个嵌套层中填充空格:

while (n > 0)
{
    for (...) // print spaces
    {
    }
    for (...) // print decreasing
    {
    }
    for (...) // print increasing
    {
    }
}

但是,您有3个嵌套图层!

for (linhas ...) // useless code - you should remove it!
{
    for (...) // this code is misplaced
    {
    }
    while (n > 0)
    {
        for (...) // print decreasing
        {
        }
        for (...) // print increasing
        {
        }
    }
}

外部循环仅在迭代时执行(因为在第二次迭代n为0,并且它退出)。这段代码是不必要的和令人困惑的 - 删除它,然后你会看到在哪里放置代码打印空格。

答案 2 :(得分:1)

我的五美分。:)

您可以使用符号*分别输出第一个数字作为格式说明符中的字段宽度和相应的数字。

例如

printf( "%*u", 2 * i + 1, j );
         ^^^ 

这是一个示范程序

#include <stdio.h>

int main( void )
{
    while ( 1 )
    {
        printf( "Enter the height of the pyramid (0- exit): " );

        unsigned int n;

        if ( scanf( "%u", &n ) != 1 || n == 0 ) break;

        printf( "\n" );

        for ( unsigned int i = 0; i < n; i++ )
        {
            unsigned int j = 1;
            printf( "%*u", 2 * i + 1, j );
            while ( !( n - i < j + 1 ) ) printf( " %u", ++j );
            while ( --j != 0 ) printf( " %u", j );
            printf( "\n" );
        }        
    }

    return 0;
}

如果要按顺序输入

9 8 7 6 5 4 3 2 1 0

然后程序输出

Enter the height of the pyramid (0- exit): 9

1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
        1 2 3 4 5 4 3 2 1
          1 2 3 4 3 2 1
            1 2 3 2 1
              1 2 1
                1
Enter the height of the pyramid (0- exit): 8

1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 6 5 4 3 2 1
    1 2 3 4 5 6 5 4 3 2 1
      1 2 3 4 5 4 3 2 1
        1 2 3 4 3 2 1
          1 2 3 2 1
            1 2 1
              1
Enter the height of the pyramid (0- exit): 7

1 2 3 4 5 6 7 6 5 4 3 2 1
  1 2 3 4 5 6 5 4 3 2 1
    1 2 3 4 5 4 3 2 1
      1 2 3 4 3 2 1
        1 2 3 2 1
          1 2 1
            1
Enter the height of the pyramid (0- exit): 6

1 2 3 4 5 6 5 4 3 2 1
  1 2 3 4 5 4 3 2 1
    1 2 3 4 3 2 1
      1 2 3 2 1
        1 2 1
          1
Enter the height of the pyramid (0- exit): 5

1 2 3 4 5 4 3 2 1
  1 2 3 4 3 2 1
    1 2 3 2 1
      1 2 1
        1
Enter the height of the pyramid (0- exit): 4

1 2 3 4 3 2 1
  1 2 3 2 1
    1 2 1
      1
Enter the height of the pyramid (0- exit): 3

1 2 3 2 1
  1 2 1
    1
Enter the height of the pyramid (0- exit): 2

1 2 1
  1
Enter the height of the pyramid (0- exit): 1

1
Enter the height of the pyramid (0- exit): 0

答案 3 :(得分:0)

你应该尝试(保留大部分代码)

//Function
int triangle (int n)
{
  int x, j, linhas, spaces;
  int orig_n = n;
//Number of lines
  for (linhas = 0; linhas < n; linhas++)
    {

      do
        {
          for (spaces = 0; spaces < (orig_n - n) * 3; spaces++)
            {
              printf (" ");
            }
//Fill in the numbers
          //Increasing part till n
          for (x = 0; x < n; x++)
            {
              printf (" %d ", (x + 1));
            }
//Decreasin part from n
          for (j = 0; j < (n - 1); j++)
            {
              printf (" %d ", ((n - 1) - j));
            }
//New line after each line
          printf ("\n");
          n--;
        }
      while (n > 0);
    }
}