我正在尝试使用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
你想得到什么输出? –
已编辑注释以显示我现在想要的输出。 – m3k3r1
等等,为什么你给函数内部的参数'n'分配新的值?这是各种麻烦的秘诀。 –