码迷,mamicode.com
首页 > 编程语言 > 详细

算法题,打印沙漏

时间:2018-09-25 15:29:03      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:har   cout   turn   while   png   end   mes   .com   ima   

#include <iostream>
using namespace std;
int sak(int z)
{
    return 1 + 4 * z + 2 * z * z;
}
int main()
{
    int x;
    char a;
    cin >> x >> a;
    //cout << x << a << endl;
    int n = 0;
    //cout << sak(n + 1) << endl;
    while(!((x >= sak(n)) && (x < sak(n + 1))))
    {
        n++;
        //cout << n << endl;
    }
    //cout << n;
    int k = n;
    int r = 0;
    for (; k != 0; k--,r++)
    {        
        int s = 2 * k + 1;
        for (int i = 0; i < r; i++)
        {
            cout << " ";
        }
        for (int j = 0; j < s; j++)
        {
            cout << a;
        }
        cout << endl;
    }
    for (int i = 0; i < r; i++)
    {
        cout << " ";
    }
    cout << a << endl;
    k++; r--;
    for (; k != n + 1; r--, k++)
    {
        int s = 2 * k + 1;
        for (int i = 0; i < r; i++)
        {
            cout << " ";
        }
        for (int j = 0; j < s; j++)
        {
            cout << a;
        }
        cout << endl;
    }

    cout << x - sak(n);
    system("pause");
    return 0;

}

技术分享图片

算法题,打印沙漏

标签:har   cout   turn   while   png   end   mes   .com   ima   

原文地址:https://www.cnblogs.com/huaye37/p/9699454.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!