Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环打印半棱锥体的C语言嵌套编程_C_For Loop_Nested - Fatal编程技术网

循环打印半棱锥体的C语言嵌套编程

循环打印半棱锥体的C语言嵌套编程,c,for-loop,nested,C,For Loop,Nested,在C编程中打印半金字塔时,我遇到了一些问题。将高度设置为7时的输出应如下所示: 1 22 333 1111 22222 333333 1111111 我的代码是: int main() { int height, i, row, j; printf("Enter the height: "); scanf("%d", &height); for (i = height; i >= 0; i--) //print the

在C编程中打印半金字塔时,我遇到了一些问题。将高度设置为7时的输出应如下所示:

       1
      22
     333
    1111
   22222
  333333
 1111111
我的代码是:

int main()
{
int height, i, row, j;

printf("Enter the height: ");
scanf("%d", &height);

for (i = height; i >= 0; i--) //print the number of rows based on input
{
    for (row = 0; row < i-1; row++) {
        printf(" ");   // print spaces for each rows
        //Edited as I just realized that I did it in the Java way. So sorry
        /*for (j = row-1; j < row; j++) {
            printf("%d", j);
        }*/
    }
    printf("\n");
}

return 0;
}

现在我设法打印出每行的倒排空格。但是我有点纠结于如何打印出每一行的数字,因为我注释掉的那一行使我的exe停止工作。

这是一个简单的问题,正确的代码是:

int main()
{
int height, i, row, j,no=1;    
printf("Enter the height: ");
scanf("%d", &height);    
for (i = height; i > 0; i--) //print the number of rows based on input
{
    if(no>3)
        no=1;//so that numbers never exceed 3
    for (row = 0; row < i-1; row++)
        printf(" ");   // print spaces for each rows    
    for(j=height+1;j>i;--j)
        printf("%d",no);//print the numbers
    no++;
    printf("\n");
}    
return 0;
}

你的注释for循环不应该嵌套,你只需要取i的值,然后从那里上升到高度

同样,循环的顶层条件应该是i>0

另一个代码

#include <stdio.h>

int main()
{
    int i, j;
    int width = 7;
    int height = 7;
    int b = 3;

    for(i = 1; height >= i; ++i) {
        for(j = 1; width >= j; ++j) {
            if(width - i < j) {
                int x = (i % b);
                printf("%d", x ? x : b);
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}
接住

#include <stdio.h>

int main( void )
{
    while ( 1 )
    {
        printf( "Enter a non-negative number (0-exit): " );

        unsigned int n = 0;
        scanf( "%u", &n );

        if ( !n ) break;

        printf( "\n" );

        for ( unsigned int i = 0; i < n; i++ )
        {
            const unsigned int k = i % 3 + 1;
            printf( "%*d", n - i, k );
            for ( unsigned int j = 0; j < i; j++ ) printf( "%d", k );
            printf( "\n" );
        }
        printf( "\n" );
    }        
}
输出结果将是

Enter a non-negative number (0-exit): 10

         1
        22
       333
      1111
     22222
    333333
   1111111
  22222222
 333333333
1111111111

Enter a non-negative number (0-exit): 7

      1
     22
    333
   1111
  22222
 333333
1111111

Enter a non-negative number (0-exit): 0

只是为了说明可以在printf中使用width参数


您阅读了printf的文档了吗?您不希望j循环嵌套在row循环中;您希望在打印完所有空格后打印数字。而且,是的,printfrow不会做任何有用的事情。想解释一下吗?我不确定第三个for循环,这就是我注释掉它的原因,错误只发生在第三个for循环,这就是为什么我从不显示错误消息。你是否清楚地阅读了如何使用printf函数downvote for第三个printf函数,这是注释谢谢!但是你介意解释一下第三个for循环吗?我感到困惑。@Denise为什么j的初始值是高度+1,因为我的初始值是高度,所以很明显,第一种情况下,当我们第二次进入同一个循环时,只会打印1个数字,我必须减少1,这意味着第三个循环现在将从高度+1运行到高度的2倍,这是我们想要的,其余部分很容易看到。我明白了,我明白了。非常感谢你的帮助!好的,非常感谢!你的也可以,但有点复杂。对不起,这条线是干什么用的?printf%*d,n-i,k;据我所知,在第一个循环中,你得到了用户输入,你得到了模块并分配给k。然后对于带有j的for循环,你显示的是k@Denise,它输出n-i-1个空格而不使用循环,并输出数字。所以基本上*d对间隔和mod做了魔术?因为我试图删除*并且返回的结果不同。您能解释一下*的作用吗?@Denise*允许使用存储所需值并作为参数传递给printf的变量指定输出字段的长度。@Denise这是这里提供的最好的代码。@Denise没问题:-
#include <stdio.h>

int main( void )
{
    while ( 1 )
    {
        printf( "Enter a non-negative number (0-exit): " );

        unsigned int n = 0;
        scanf( "%u", &n );

        if ( !n ) break;

        printf( "\n" );

        for ( unsigned int i = 0; i < n; i++ )
        {
            const unsigned int k = i % 3 + 1;
            printf( "%*d", n - i, k );
            for ( unsigned int j = 0; j < i; j++ ) printf( "%d", k );
            printf( "\n" );
        }
        printf( "\n" );
    }        
}
10 7 0
Enter a non-negative number (0-exit): 10

         1
        22
       333
      1111
     22222
    333333
   1111111
  22222222
 333333333
1111111111

Enter a non-negative number (0-exit): 7

      1
     22
    333
   1111
  22222
 333333
1111111

Enter a non-negative number (0-exit): 0
#include <stdio.h>

int main() {
    int height, j, n = 0;

    printf("Enter the height: ");
    scanf("%d", &height);

    for (j = 0; j < height; n *= 10)
    {
        printf("%*d\n", height+1, ++n * (1 + j++ % 3));
    }

    return 0;
}