PTA 打印沙漏

https://pintia.cn/problem-sets/17/problems/260

 

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int N;
    char sign;
    cin >> N >> sign;
    int count = 1;
    while (2 * count * count - 1 <= N)
    {
        count++;
    }
    count--;

    for (int i = 0; i < 2 * count - 1; i++)
    {
        for (int j = 0; j < count - 1 - abs(count - 1 - i); j++)
        {
            printf(" ");
        }
        for (int j = 0; j < 2 * abs(count - 1 - i) + 1; j++)
        {
            printf("%c", sign);
        }
        printf("\n");
    }
    printf("%d", N - (2 * count * count - 1));
    return 0;
}

 

posted @ 2019-02-17 11:03  Ruohua3kou  阅读(310)  评论(0编辑  收藏  举报