poj 1941 The Sierpinski Fractal 递归

//poj 1941
//sep9
#include <iostream>
using namespace std;
const int maxW=2048;
const int maxH=1024;
int pow2[32];
char g[maxH+10][maxW+10];
void print(int x,int y,int n)
{
	if(n==1){
		g[x][y+1]='/';
		g[x][y+2]='\\';
		g[x+1][y]='/';
		g[x+1][y+1]='_';
		g[x+1][y+2]='_';
		g[x+1][y+3]='\\';
		return ;
	}
	print(x,y+pow2[n-1],n-1);
	print(x+pow2[n-1],y,n-1);
	print(x+pow2[n-1],y+pow2[n],n-1);
	return ;	
}

int main()
{
	pow2[0]=1;
	for(int i=1;i<30;++i)
		pow2[i]=pow2[i-1]*2;
	int n;
	while(scanf("%d",&n)==1&&n){
		int H=pow2[n];
		int W=pow2[n+1];
		memset(g,' ',sizeof(g));
		print(0,0,n);
		for(int i=0;i<H;++i){
			for(int j=0;j<W;++j)
				printf("%c",g[i][j]);
			printf("\n");
		}
		puts("");
	}		
	return 0;	
}


posted @ 2016-04-07 17:12  zfyouxi  阅读(447)  评论(0编辑  收藏  举报