空心的数字金字塔
函数练习题
本题要求实现一个函数,输出n行空心的数字金字塔。
函数接口定义:
void hollowPyramid( int n );
其中n
是用户传入的参数,为[1, 9]的正整数。要求函数按照如样例所示的格式打印出n
行空心的数字金字塔,请注意,最后一行的第一个数字前没有空格。
裁判测试程序样例:
#include <stdio.h>
void hollowPyramid ( int n );
int main()
{
int n;
scanf("%d", &n);
hollowPyramid ( n );
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
5
结尾无空行
输出样例:
1
2 2
3 3
4 4
555555555
结尾无空行
代码示例如下:
void hollowPyramid ( int n )
{
int i,j,k,z;
for(i=1;i<=n;i++)
{
for(j=n-i;j>0;j--)
{
printf(" ");
}
printf("%d",i);
if(i==1) printf("\n");
if(i>=2&&i<n)
{
for(k=0;k<2*i-3;k++)
{
printf(" ");
}
printf("%d\n",i);
}
}
i=i-1;
if(i==n)
{
for(z=0;z<2*n-2;z++)
{
printf("%d",i);
}
}
return;
}