2017-03-01 104 views
-3

我想打印两个半座金字塔是这样一个双过半金字塔:打印用C

http://i.imgur.com/yze8MlI.png

到目前为止我的代码看起来是这样的

#include <stdio.h> 

int main(void) { 
    int row, star, space, n; 
    printf("Enter row number:"); 
    scanf("%d", &n); 
    for (row = 1; row <= n; row++) { 
     for (space = n - 1; space >= row; space--){ 
      printf(" "); 
     } 
     for (star = 1; star <= row; star++) { 
      printf("##"); 
     } 
     printf("\n"); 
    } 
    return 0; 
} 

,但它打印整个金字塔,我如何在中间实施一个空间?

回答

0
for(star=1;star<=row*2;star++){ 

     printf("#"); 
    if(star == row) 
    printf(" "); 

} 

修改第二环路如上

1
#include <stdio.h> 

int main(void) { 
    int height=5; 
    const char* bricks = "###############" 
    for(int i=1; i<height+1; ++i) 
    { 
     printf("%*.*s  %.*s\n", height,i,bricks, i,bricks); 
    } 
    return 0; 
} 

IDEOne Link

输出:

#  # 
    ##  ## 
    ###  ### 
####  #### 
#####  ##### 
0

以下代码是简单易懂给你完全控制在金字塔上:

你也可以改变高度(行数)和半圆锥体的大小(在例子-4中)。然后它将*打印为图像中的砖块,spaces作为空间,##作为它们之间的空间。

#include <stdio.h> 

int main(void) 
{ 
    int row,col,space,n1=4, n2=4; 

    for(row=1;row<=n1;row++){ 

    for(space=n2-row;space>=0;space--) 
     printf(" "); 

    for(col=1;col<=row;col++) 
     printf("*"); 

    printf("##"); 

    for(col=1;col<=row;col++) 
     printf("*");  

    for(space=n2-row;space>=0;space--) 
     printf(" "); 

    } 

    printf("\n"); 
} 

输出示例:

*##*  
    **##** 
    ***##*** 
****##****