沙雕金字塔
函数接口定义:
void pyramid( int n );
其中n是用户传入的参数,为[1, 9]的正整数。要求函数按照如样例所示的格式打印出n行数字金字塔。注意每个数字后面跟一个空格。
#include <stdio.h>
#include <windows.h>
void pyramid(int n)
{
int a = 1, b = 1,c = n ;
for (a; a <= n; a++)
{
for (c; c >= 0; c--)
{
printf(" ");
}
c = n - a;
for (b; b <= a; b++)
{
printf("%d ",a);
}
printf("\n");
b = 1;
}
}
int main()
{
int n,m = 0;
while (m == 0)
{
scanf_s("%d", &n);
if (n > 9)
{
printf("请输入9以下的数字\n");
m = 0;
}
else
m = 1;
}
pyramid(n);
}