打印金字塔代码

Description

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

Input

输入只有一个正整数n。

Output

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

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int n,i,j;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=2*(n-i);j++)
			cout<<" ";
		for(j=1;j<=i;j++)
		{
			if(i==1)
				cout<<j;
			else
				cout<<j<<" ";
		}
		for(j=i-1;j>=1;j--)
		{
			if(j!=1)
				cout<<j<<" ";
			else
				cout<<j;
		}
		if(i!=n)
			cout<<endl;
	}
	return 0;
}


 

 

posted @ 2013-05-06 19:30  javawebsoa  Views(544)  Comments(0Edit  收藏  举报