2014-10-27 82 views
-13

嗨,我有這個程序來解決一個完整的金字塔。C程序打印全金字塔

#include <stdio.h> 
int main() 
{ 
    int i,j,rows; 
    printf("Enter the number of rows: "); 
    scanf("%d",&rows); 
    for(i=1;i<=rows;++i) 
    { 
     for(j=1;j<=i;++j) 
     { 
      printf("%d ",j); 
     } 
     printf("\n"); 
    } 
    return 0; 
} 

輸出是這

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

我怎樣才能打印此像一個完整的金字塔?再次從左側打印相同的東西。

所需的輸出:

1 1 
    12 12 
123 123 
1234 1234 
+2

我不明白你通過一個完整的金字塔的意思。你可以發佈一個期望的輸出的例子嗎? – 2014-10-27 22:17:56

+0

@JasonBakeri編輯後,我想要的輸出是有什麼辦法張貼我想要的輸出圖片嗎?謝謝 – inferno123 2014-10-27 22:27:01

+0

@JasonBaker我想再次打印相同的輸出,但與space..like一個三角形從左邊是這個數字,右邊是一個,就像輸出 – inferno123 2014-10-27 22:34:51

回答

1

我覺得你在尋找這樣的事情:

#include <stdio.h> 

int main() { 

    int rowCount, numberCount, rows; 

    printf("Enter the number of rows: \n>"); 
    scanf(" %d", &rows); 

    for(rowCount = 1; rowCount <= rows; rowCount++) { 

     for(numberCount = rowCount; numberCount < rows; numberCount++) 
      printf(" "); 

     for(numberCount = rowCount; numberCount >= 1; numberCount--) 
      printf("%d",numberCount); 

     printf(" "); 

     for(numberCount = 1; numberCount <= rowCount; numberCount++) 
      printf("%d",numberCount); 

     printf("\n"); 
    } 


    return 0; 

} 
+0

您是否看到了期望的輸出?我如何發佈我想要的輸出?我有輸出圖像我如何在這裏上傳圖像?它說我可以'因爲我的聲望不低於十... – inferno123 2014-10-27 23:10:04

+0

@ spl19只是看到它並更新它!現在它就像你想要的! – Rizier123 2014-10-27 23:14:22