题目描述
本题要求实现一个函数,输入一个正整数n(1<=n<=9),输出n行空心的数字金字塔。要求定义和调用函数hollow_pyramid(n)打印出n行空心的数字金字塔。
输入
正整数n(1<=n<=9) (eg:5)
输出
1
2 2
3 3
4 4
555555555
#include <stdio.h>
hollow_pyramid ( int n );
int main()
{
int n;
scanf("%d", &n);
hollow_pyramid ( n );
return 0;
}
hollow_pyramid ( int n )
{
int i,j;
for(i=1;i<=n;i++)
{ //i为行数
for(j=1;j<=n-1+i;j++)
{ //j为第i行中的第几个 (j的结束条件是本行非空格的后一个数字)
if(i==n)
{
printf("%d",i);
}
else if(i<n)
{
if((j == n + 1 - i)||(j == n - 1 + i))
{
printf("%d",i);
}
else
{
printf(" ");
}
}
}
printf("\n");
}
}