2016-08-31 39 views
0

我想建立左半金字塔如何建立半左上角金字塔在C

 ## 
     ### 
    #### 
    ##### 
    ###### 
    ####### 
######## 
######### 

這裏是我的代碼:

#include <stdio.h> 
#include <cs50.h> 

int main(void) 
{ 
    printf("Enter the height of the pyramid:"); 
    unsigned int height = GetInt(); 
    int counter; 

    while(height <1 || height >23){ 
     printf("Incorect number,try again:"); 
     height = GetInt(); 
    } 

    for(counter = 2;counter <=height+1;++counter) { 
     printf("%.*s\n", counter, "##############################"); 
    } 



} 

我建金字塔,而是離開角落我得到了右角金字塔。 我怎樣才能把它轉到左邊?

P.S我知道,在這種情況下使用printf並不是最好的主意,但它告訴我用這個命令創建代碼。

+6

'的printf( 「%* * S \ n」,身高+ 1,計數器,...' – BLUEPIXY

+0

@BLUEPIXY謝謝,但是,你能解釋一下這個coude做什麼,我的意思是每個輸入(特別是第一個輸入) –

+0

參見[printf](http://en.cppreference.com/w/c/io/fprintf ) – BLUEPIXY

回答

0

試試這個:

​​

輸出:

How many lines long ? 
10 

     # 
     ## 
     ### 
     #### 
    ##### 
    ###### 
    ####### 
    ######## 
######### 
########## 


#include <stdio.h> 

int main() 
{ 
    int n,i,j,k; 
    printf("How many lines long ?\n"); 
    scanf("%d",&n); 
    n++; 
    for(i = 0; i<=n; i++) 
    { 
     for(j = 0; j<n-i; j++) 
     { 
      printf(" "); 
     } 
     for(k = 0; k<i; k++) 
     { 
      if(i == 1) 
      { 
       continue; 
      } 
      printf("#"); 
     } 
     printf("\n"); 
    } 
    return 0; 
} 

Output: 
How many lines long ? 
10 


     ## 
     ### 
     #### 
     ##### 
    ###### 
    ####### 
    ######## 
    ######### 
########## 
###########