C语言实验——打印金字塔

C语言实验——打印金字塔

Time Limit: 1000 ms  Memory Limit: 65536 KiB

Problem Description

输入n值,打印下列形状的金字塔,其中n代表金字塔的层数。 

Input

输入只有一个正整数n。

Output

打印金字塔图形,其中每个数字之间有一个空格。

Sample Input

3

Sample Output

    1
  1 2 1
1 2 3 2 1

Hint

Source


注意:数后面的空格不能用“%d ”来表示,要在输出数字时判断,符合条件就加上空格。

        并且最右边的数字1后面应该没有空格。


#include<stdio.h>
int main(){
	int i,j,k;
	int n;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		for(j=1;j<=2*(n-i);j++){
			printf(" ");
		}
		for(k=1;k<=i;k++){
			printf("%d",k);
			if(i!=1){
				printf(" ");
			}
		}
		for(j=i-1;j>0;j--){
			printf("%d",j);
			if(j!=1){
				printf(" ");
			}
		}
		printf("\n");
	}
}

猜你喜欢

转载自blog.csdn.net/ailmengi000/article/details/79918632