递归 谢尔宾斯基三角形

好吧,WA了总共接近两个小时,发现是数组开太小了,但是2的10次方不应该是1024吗???
一开始傻逼没想到用数组存,直接打印想了巨久。naive。

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
char triangle[2048][2048];
void draw(int depth, int posx, int posy) {
	if (depth == 1) {
		triangle[posy][posx-1]='/';
		triangle[posy][posx]='\\';
		triangle[posy+1][posx-2]='/';
		triangle[posy+1][posx-1]='_';
		triangle[posy+1][posx]='_';
		triangle[posy+1][posx+1]='\\';	
	}
	else {
		int side=pow(2,depth);
		draw(depth-1,posx,posy);
		draw(depth-1,posx-side/2,posy+side/2);
		draw(depth-1,posx+side/2,posy+side/2);
	}
}
void print(int depth) {
	int side = pow(2,depth);
	for (int i = 1; i <= side; ++i) {
		for (int j = 1025-side; j < 1025+i; ++j) {
			cout << triangle[i][j];
		}
		cout << endl;
	}
}
int main() {
	memset(triangle,' ',sizeof(triangle));
	draw(10,1025,1);
	//cout <<
	int n;
	cin >> n;
	while(n) {
		print(n);
		cout << endl;
		cin >> n;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44288817/article/details/90338899