如何使用for循环打印星形金字塔在这种C语言中

时间:2015-04-11 18:25:25

标签: c

这是我的代码,我无法打印金字塔请帮助...

for(i=1;i<=5;i+=2){
   for(j=4;j>=i;j--)
       printf(" ");
   for(k=1;k<=i;k++)
       printf("*");
   printf("\n");
}

3 个答案:

答案 0 :(得分:1)

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../openssl/include
LOCAL_STATIC_LIBRARIES := libssl libcrypto

答案 1 :(得分:0)

你的代码 打印一个金字塔,只有一个小mod评论过。

#include <stdio.h>

int main(void)
{
   int i, j, k;
    for(i=1;i<=5;i+=2){
       for(j=4;j>=i;j-=2)   // loop by minus 2
           printf(" ");
       for(k=1;k<=i;k++)
           printf("*");
       printf("\n");
    }
    return 0;
}

节目输出:

  *
 ***
*****

答案 2 :(得分:0)

这是我的金字塔

#include <stdio.h>

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

        size_t n = 0;
        scanf( "%zu", &n );

        if ( !n ) break;

        printf( "\n" );

        for ( size_t i = 0; i < n; i++ )
        {
            printf( "%*c", n - i, '*' );
            for ( size_t j = 0; j < i; j++ ) printf( " %c", '*' );
            printf( "\n" );
        }
    }

    return 0;
}

如果要按顺序输入5,4,3,2,1,0,则程序输出为

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

    *
   * *
  * * *
 * * * *
* * * * *
Enter the height of the pyramid (0-exit): 4

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

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

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

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