输出字符菱形

编程输入字符X,输出由字符X构成的以下样式的字符图形。

输入样例:

*

输出样例:

   *
  ***
 *****
*******
 *****
  ***
   *

输入样例:

A

输出样例:

   A
  AAA
 AAAAA
AAAAAAA
 AAAAA
  AAA
   A

突然想到一个比较方便的

最中间那一竖条先不看,看左边空白,从上至下

分别是

3个空白

2个空白

1个空白

0个空白

1个空白

2个空白

3个空白

相应的A也就是3-n个

#include <cstdio>
#include "vector"
#include "iostream"
#include "map"
#include "algorithm"
#include "math.h"

using namespace std;

int main() {
//    freopen("../i.txt", "r", stdin);
    char e;
    cin >> e;
    for (int i = 0; i < 7; ++i) {
        if (i!=0)
            cout << endl;
        for (int j = 0; j < abs(3 - i); ++j) {
            cout << ' ';
        }
        for (int j = 0; j < (3 - abs(3 - i)) * 2 + 1; ++j) {
            cout << e;
        }
    }
    return 0;
}

 进而想到通用的

菱形图案

请编程输入一个奇数n(n<100)和一个字符c,输出n行由字符c组成的菱形图案。

输入样例:

5 A

输出样例:

  A
 AAA
AAAAA
 AAA
  A

输入样例:

13 ?

输出样例:

      ?
     ???
    ?????
   ???????
  ?????????
 ???????????
?????????????
 ???????????
  ?????????
   ???????
    ?????
     ???
      ?


 

 假设左边这里叫做halfWidth,则halfWidth=(行数- 1) / 2

那么有

 最中间那一竖条先不看,看左边空白,从上至下

分别是

第0行,abs(halfWidth-0)个空白

第1行,abs(halfWidth-1)个空白

第2行,abs(halfWidth-2)个空白

第3行,abs(halfWidth-3)个空白

第4行,abs(halfWidth-4)个空白

第5行,abs(halfWidth-5)个空白

第6行,abs(halfWidth-6)个空白

相应的A也就是(halfWidth - abs(halfWidth - 行号)) * 2 + 1个

#include <cstdio>
#include "vector"
#include "iostream"
#include "map"
#include "algorithm"
#include "math.h"

using namespace std;

int main() {
//    freopen("../i.txt", "r", stdin);
    char e;
    int line;
    cin >> line;
    cin >> e;
    int halfWidth = (line - 1) / 2;
    for (int i = 0; i < line; ++i) {
        if (i != 0)
            cout << endl;
        for (int j = 0; j < abs(halfWidth - i); ++j) {
            cout << ' ';
        }
        for (int j = 0; j < (halfWidth - abs(halfWidth - i)) * 2 + 1; ++j) {
            cout << e;
        }
    }
    return 0;
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值