链接:http://poj.org/problem?id=2083
题目描述:
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.
输入描述:
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.
输出描述:
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.
题意:
按照案例输出n阶分形图
数据范围:
n <= 7
思路:
利用递归
在左上 右上 左下 右下 中间 分别放置一个n-1
代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
char map[1000][1000];
void print(int t,int x,int y)
{
int m;
if(t == 1)
{
map[x][y] = 'X';
return ;
}
m = pow(3.0,t-2);
print(t-1,x,y);
print(t-1,x,y+2*m);
print(t-1,x+m,y+m);
print(t-1,x+2*m,y);
print(t-1,x+2*m,y+2*m);
}
int main()
{
int t;
while(scanf("%d",&t)&&t!=-1)
{
int m ;
m = pow(3.0,t-1);
for(int i = 1;i <= m;i++)
for(int j = 1;j <= m;j++)
map[i][j] = ' ';
print(t,1,1);
for(int i = 1;i <= m;i++)
{
for(int j = 1;j <= m;j++)
printf("%c",map[i][j]);
printf("\n");
}
printf("-\n");
}
return 0;
}