1027 打印沙漏 (20 分)

额,反正是水题就对了。

  1. 先确定层数
  2. 分别输出上半部和下半部
  3. 计算剩下的数目,简单的等差数列运算。
int n;
char c;

int main()
{
    cin>>n>>c;

    int s=(n+1)/2;
    int dep=0;
    for(int i=1;;i++)
    {
        s-=2*i-1;
        if(s < 0) break;
        dep++;
    }

    for(int i=1;i<=dep;i++)
    {
        for(int j=0;j<i-1;j++) cout<<' ';
        for(int j=0;j<2*(dep-i)+1;j++) cout<<c;
        cout<<endl;
    }

    for(int i=dep-1;i>=1;i--)
    {
        for(int j=0;j<i-1;j++) cout<<' ';
        for(int j=0;j<2*(dep-i)+1;j++) cout<<c;
        cout<<endl;
    }

    cout<<n-dep*dep*2+1<<endl;
    //system("pause");
    return 0;
}
posted @ 2021-02-12 21:39  Dazzling!  阅读(37)  评论(0编辑  收藏  举报