練習5-3 數字金字塔 (15分)
阿新 • • 發佈:2021-01-09
練習5-3 數字金字塔 (15分)
本題要求實現函式輸出n行數字金字塔。
函式介面定義:
void pyramid( int n );
其中n是使用者傳入的引數,為[1, 9]的正整數。要求函式按照如樣例所示的格式打印出n行數字金字塔。注意每個數字後面跟一個空格。
裁判測試程式樣例:
#include <stdio.h>
void pyramid( int n );
int main()
{
int n;
scanf("%d", &n);
pyramid(n);
return 0;
}
/* 你的程式碼將被嵌在這裡 */
輸入樣例:
5
輸出樣例:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
AC
我找規律找了好久啊。。。。
void pyramid( int n ) {
int i,j,k;
int cnt=n-1;
for(i=1; i<=n; i++) {
for(j=cnt; j>0; j--)
printf(" ");
cnt--;
for(j=1; j<=i; j++)
printf("%d ",i);
printf ("\n");
}
}