程式說明
金字塔是透過連接多邊形底面和稱為頂點的點所形成的多面體。每個底邊和頂點形成一個三角形,稱為側面。它是一個底面為多邊形的圓錐體。有 n 邊底的金字塔有 n 1 個頂點、n 1 個面和 2n 個邊。所有金字塔都是自對偶的。

演算法
1 2 3 4 | Accept the number of rows from the user to form pyramid shape
Iterate the loop till the number of rows specified by the user:
Display 1 star in the first row
Increase the number of stars based on the number of rows.
|
登入後複製
範例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # include <stdio.h>
int main() {
int r, s, rows=0;
int t=0;
clrscr();
printf( "Enter number of rows to print the pyramid: " );
scanf( "%d" , &rows);
printf( "</p><p>" );
printf( "The Pyramid Pattern for the number of rows are:" );
printf( "</p><p></p><p>" );
for (r=1;r<=rows;++r,t=0) {
for (s=1; s<=rows-r; ++s){
printf( " " );
}
while (t!=2*r-1) {
printf( "* " );
++t;
}
printf( "</p><p>" );
}
getch();
return 0;
}
|
登入後複製
輸出

以上是在C語言中編寫一個列印金字塔圖案的程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!