C编程嵌套为循环打印半金字塔

时间:2015-08-17 12:11:56

标签: 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 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停止工作。

5 个答案:

答案 0 :(得分:2)

抓住:!)

#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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

您的评论for循环不应该嵌套,您只需要取$ids = explode(",",$_POST["scids"]); foreach ($ids as $id) { // Do something with the array of ids. }; 的值并从那里开始到i

此外,顶级for循环中的条件应为height

i > 0

输出:

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; row++) {
            printf(" ");
        }
        for (j = i; j <= height; j++) {
            printf("%d", (height - i) % 3 + 1);
        }
        printf("\n");
    }

    return 0;
}

答案 3 :(得分:1)

另一个代码。

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

答案 4 :(得分:0)

只是为了表明您可以使用printf中的width参数。

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