http://poj.org/problem?id=2083
递归,分治思想。
#include<stdio.h> char box[730][730]={"",""}; void drawFractalBox(int n,int x,int y) { int i=n,c=1; while(--i) c=c*3; if(c==1) { box[x][y]='X'; return; } else { drawFractalBox(n-1,x,y); drawFractalBox(n-1,x,(c*2)/3+y); drawFractalBox(n-1,c/3+x,c/3+y); drawFractalBox(n-1,(c*2)/3+x,y); drawFractalBox(n-1,(2*c)/3+x,y+(2*c)/3); } } int main(void) { int i,j,n,num,t; while(scanf("%d",&n),n!=-1) { num=1; t=n; while(--t) num=num*3; for(i=1;i<=num;i++) for(j=1;j<=num;j++) box[i][j]=' '; drawFractalBox(n,1,1); for(i=1;i<=num;i++) { for(j=1;j<=num;j++) printf("%c",box[i][j]); printf("\n"); } printf("-\n"); } return 0; }