蓝桥杯 历届试题 PREV-2 打印十字图

参考图:https://blog.csdn.net/jopus/article/details/19091643

#include <iostream>
#define D(x) cout<<#x<<"="<<x<<endl;
using namespace std;

int main() {
	int a;// = 3;
	cin >> a;
	int row = 5 + 2 * a * 2;
	int col = row;
	//动态开辟空间
	char **p = new char*[row]; //开辟行
	for (int i = 0; i < row; i++)
		p[i] = new char[col]; //开辟列

	//0、初始化
	for (int i = 0 ; i < row ; i++)
		for (int j = 0; j < col; j++)
			p[i][j] = '.';

	//1、中间的十字
	int center = row / 2;
	for (int i = center - 2; i <= center + 2; ++i) {
		p[center][i] = '$';
		p[i][center] = '$';
	}

	//2、从里向外打印边框,每次+2
	int add = 4;
	// int frame=center-add;
	while (center - add >= 0) {
		for (int i = center - add + 2; i <= center + add - 2; ++i) {
			//横两线
			p[center - add][i] = '$';
			p[center + add][i] = '$';

			//竖两线
			p[i][center - add] = '$';
			p[i][center + add] = '$';

		}
		//四个角,每个角是三个点
		//四个拐点
		p[center - add + 2][center - add + 2] = '$';
		p[center - add + 2][center + add - 2] = '$';
		p[center + add - 2][center - add + 2] = '$';
		p[center + add - 2][center + add - 2] = '$';



		p[center - add + 2 - 1][center - add + 2] = '$';
		p[center - add + 2 - 1][center + add - 2] = '$';
		p[center + add - 2 + 1][center - add + 2] = '$';
		p[center + add - 2 + 1][center + add - 2] = '$';


		p[center - add + 2][center - add + 2 - 1] = '$';
		p[center - add + 2][center + add - 2 + 1] = '$';
		p[center + add - 2][center - add + 2 - 1] = '$';
		p[center + add - 2][center + add - 2 + 1] = '$';


		add += 2;
		// frame=center-add;;
	}


	//输出
	for (int i = 0 ; i < row ; i++) {
		for (int j = 0; j < col; j++) {
			// cout << p[i][j] << ' ';
			cout << p[i][j];
		}
		cout << endl;
	}

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值