C语言实验——打印金字塔 |
---|
Time Limit: 1000 ms Memory Limit: 65536 KiB |
Problem Description
输入n值,打印下列形状的金字塔,其中n代表金字塔的层数。
Input
打印金字塔图形,其中每个数字之间有一个空格。
Sample Input
3
Sample Output
1
1 2 1
1 2 3 2 1
参考代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j;
int n;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
for(j=i; j<n; j++)
printf(" ");
for(j=1; j<=i; j++)
{
printf("%d",j);
if(i!=1)
printf(" ");
}
for(j=i-1; j>0; j--)
{
printf("%d",j);
if(j!=1)
printf(" ");
}
printf("\n");
}
return 0;
}
心得体会
这个题的难点部分就是在处理空格上。要正确理解for循环的执行过程。嵌套的for循环,循环体是逐句执行的。