题面 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.
Sample
1
2
3
4
-1
X
-
X X
X
X X
-
X XX X
XX
X XX X
X X
X
X X
X XX X
XX
X XX X
-
X XX XX XX X
XXXX
X XX XX XX X
X XX X
XX
X XX X
X XX XX XX X
XXXX
X XX XX XX X
X XX X
XX
X XX X
X X
X
X X
X XX X
XX
X XX X
X XX XX XX X
XXXX
X XX XX XX X
X XX X
XX
X XX X
X XX XX XX X
XXXX
X XX XX XX X
-
思路 【算法|Fractal解题笔记】这道题显然是个递归问题,需要我们找到递归方程,不难发现分形X的相对位置是确定的,我们可以由此推出每一层位置的递归关系;当然另一个角度看,也可以看作图像的平移。为了数组处理方便,我们使用左上角为基点就可以得到一组递归方程,如果不用左上角为基点,就要涉及原点坐标的平移变换,相对麻烦些。
AC代码
#include
#include
#include
#include
using namespace std;
int n, W, cnt;
char s[1001][1001];
void b(int d, int x, int y){
if(d==1)s[y][x]='X';
else {
int size = 1;
for(int i=2;
i0;
j--)
if(s[i][j-1]=='X'){
s[i][j]='\0';
break;
}
printf("%s\n",s[i]);
}
printf("-\n");
scanf("%d", &n);
}while(n != -1);
}
推荐阅读
- 动态规划|符环 解题笔记
- leetcode刷题记录|移动零——LeetCode283题
- leetcode刷题记录|反转字符串——LC344题
- leetcode刷题记录|两数之和II - 输入有序数组——LC167题(中等难度)
- 最小生成树-克鲁斯卡尔算法(Kruskal算法)
- leetcode刷题记录|轮转数组——LeetCode198题
- IB2070数学分析
- 笔记|BYOL(Bootstrap your own latent A new approach to self-supervised Learning)算法笔记
- 37242 Optimisation