2015-11-28 236 views
1

我正在尝试使用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 
+0

你想得到什么输出? –

+0

已编辑注释以显示我现在想要的输出。 – m3k3r1

+0

等等,为什么你给函数内部的参数'n'分配新的值?这是各种麻烦的秘诀。 –

回答

2

所以,问题是在做循环。你减少计数(变量n),保持循环,直到n> 0和'空间for'执行一次。试着这样做。

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); 
    } 
} 
+0

最好不要使用'n'作为迭代器,因为它是一个参数。 –

+0

你是对的!编辑! –

+0

谢谢我已经理解我的错误。 – m3k3r1

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,并且它退出)。这段代码是不需要的,也是令人困惑的 - 删除它,然后你会看到你的代码打印空间的位置。

+0

我还在学习c我认为我仍然不使用最正确的方法,但我不会从现在开始嵌套如此多的图层 – m3k3r1

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