Recursive Sierpinski triangle

Okay, WA a total of nearly two hours, found to be too small to open the array, but the 10 th power of 2 should not it be 1024? ? ?
I did not expect to start a sucker with an array of deposit, direct printing like a giant long. 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;
}

Guess you like

Origin blog.csdn.net/weixin_44288817/article/details/90338899