前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1027 打印沙漏 (20 分)

1027 打印沙漏 (20 分)

作者头像
可爱见见
发布2019-09-09 15:57:11
7550
发布2019-09-09 15:57:11
举报
文章被收录于专栏:卡尼慕卡尼慕

1027 打印沙漏 (20 分)

我的代码

代码语言:javascript
复制
// 1027 打印沙漏 (20 分).cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <math.h>
using namespace std;
int main(){
    int i;
    cin >> i;
    char s;
    cin >> s;
    int num_total = 0;
    int number_print = 0;
    //算出第一行的个数
    for (int j = 1; j < i / 2 - 1 ; j++) {
        num_total = j * j * 2 - 1;
        if (i <= num_total)
            number_print++;
    }
    if (!number_print) {
        cout << s << endl;
        cout << i - 1;
        return 0;
    }
    number_print = sqrt(number_print) + 1;//包含1的层数
    //正序打印
    for (int i = number_print - 1; i >= 0; i--) {
        for (int j = 0; j < 2 * (i) + 1; j++){
            j == 2 * (i) ? cout << s << endl : cout << s;
        }
        if (i == 0)
            break;
        for (int line = 0; line < number_print - i; line++) {
            cout << " ";
        }
    }
    //逆序打印
    int number_pace = number_print - 1;
    for (int i = 1; i < number_print; i++) {
        for (int line = 1; line < number_pace; line++) {
            cout << " ";
        }
        for (int j = 0; j < 2 * i + 1; j++) {
             cout << s;
        }
        if (i != number_print - 1)
            cout << endl;
        number_pace--;
    }
    //计算差值
    int size = number_print * number_print * 2 - 1;
    if(i - size)
        cout << "\n" << i - size;
}

思路

没什么难度,就是要注意好空格,换行的情况。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-09-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 卡尼慕 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档