This question requires the realization of the function output n-line digital pyramid.
Function interface definition:
void pyramid( int n );
Where n is the parameter passed in by the user, which is a positive integer of [1, 9]. The function is required to print out the n-line digital pyramid in the format shown in the example. Note
It means that each number is followed by a space.
Sample referee test procedure:
#include <stdio.h>
void pyramid( int n );
int main()
{
int n;
scanf("%d", &n);
pyramid(n);
return 0;
}
/* 你的代码将被嵌在这里 */
Input sample:
5
Sample output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
answer:
void pyramid( int n )
{
int i,s,j; //循环变量
for(i=1;i<=n;i++) //循环到n(有几行循环几行)
{
for(j=1;j<=n-i;j++) //打印前面空格(空格公式需数数,最后一排是没空格第一排在第五个位置打印的所以n-i)
{
printf(" ");
}
for(s=1;s<=i;s++) //打印数字后跟一空格
{
printf("%d ",i);
}
printf("\n"); //每行打印完需换行
}
}
/*
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
*/