ZOJ - 2423-Fractal
A fractal is an object or quantity that displays self-similarity, in a somewhat technical sense, on all scales. The object need not exhibit exactly the same structure at all scales, but the same "type" of structures must appear on all scales.
A box fractal is defined as below :
- A box fractal of degree 1 is simply
X
- A box fractal of degree 2 is
X X X X X
- If using B(n - 1) to represent the box fractal of degree n - 1, then a box fractal of degree n is defined recursively as following
B(n - 1) B(n - 1) B(n - 1) B(n - 1) B(n - 1)
Your task is to draw a box fractal of degree n.
Input
The input consists of several test cases. Each line of the input contains a positive integer n which is no greater than 7. The last line of input is a negative integer -1 indicating the end of input.
Output
For each test case, output the box fractal using the 'X' notation. Please notice that 'X' is an uppercase letter. Print a line with only a single dash after each test case. Don't output any trailing spaces at the end of each line, or you may get an 'Presentation Error'!
题意:输出n层"X"型分型,遇到-1时结束输入,可以看成一个3X3的正方形X为原型,输出后还要在另一行输出一个"-"
注意:图形最右方"X"后的空格不能输出,这里给出的一个解决方法是单独把他们标记出来,输出时检测这些标记来控制输出格式
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define ll long long 5 using namespace std; 6 const int amn=1e3+10; 7 int a=3,b; 8 char mp[amn][amn]; 9 char ans[amn][amn]; 10 void solve(int x,int y,int tot) 11 { 12 if(tot==1) 13 { 14 ans[x][y]='X'; 15 return; 16 } 17 int tes=tot/a; 18 for(int i=x; i<tot+x; i+=tes)///注意这里i=x,j=y,找了好久才发现错误.... 19 { 20 for(int j=y; j<tot+y; j+=tes) 21 { 22 if(mp[(i-x)/tes][(j-y)/tes]=='X') 23 { 24 solve(i,j,tes); 25 } 26 } 27 } 28 } 29 int main() 30 { 31 32 while(~scanf("%d",&b)&&b!=-1) 33 { 34 for(int i=0; i<3; i++) 35 { 36 for(int j=0; j<3; j++) 37 { 38 if(((i==0||i==2)&&(j==0||j==2))||(i==1&&j==1))mp[i][j]='X'; 39 else mp[i][j]=' '; 40 } 41 } 42 int c=1;///注意初始化 43 for(int i=1; i<b; i++)c*=a; 44 for(int i=0; i<c; i++) 45 for(int j=0; j<c; j++) 46 ans[i][j]=' '; 47 solve(0,0,c); 48 for(int i=0; i<c; i++) 49 { 50 for(int j=c-1; j>=0; j--)///这里是从每一行的最后一个元素开始检测 51 { 52 if(ans[i][j]==' ')///将空格标记,遇到第一个"X"时跳出本次循环 53 ans[i][j]='*'; 54 else break; 55 } 56 } 57 for(int i=0; i<c; i++) 58 { 59 for(int j=0; j<c; j++) 60 { 61 if(ans[i][j]!='*')///如果这个不是被标记的元素则输出 62 printf("%c",ans[i][j]); 63 else break; 64 } 65 66 printf("\n"); 67 } 68 printf("-\n"); 69 } 70 }