题目分析:这一题主要考得是循环的运用,比较基础。
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
char a;
while(cin>>n>>a)//保证多组数据输入
{
int h;
h=sqrt((n+1)/2);//由2*h*h-1=n,算出不算沙漏下面,上面需要多少行。
int c=0;
for(int i=h;i>0;i--)
{
for(int j=1;j<=h-i;j++)
{
cout<<" ";//打印空格
}
for(int j=2*i-1;j>=1;j--)
{
cout<<a;//打印字符
}
cout<<endl;//每行回车
}
for(int i=2;i<=h;i++)//从第二个开始打印
{
for(int j=h-i;j>=1;j--)
{
cout<<" ";//打印空格
}
for(int j=1;j<=2*i-1;j++)
{
cout<<a;
}
cout<<endl;
}
c=(n-(2*h*h-1));
cout<<c;
}
return 0;
}