Problem description
The fractal of the box with a degree of 1 is simple:
X
The fractal of the box with a degree of 2 is this:
x x
X
x x
If a box fractal with a degree of n-1 is expressed in B (n-1), then the box fractal of degree n can be defined recursively as follows:
B (n-1) b (n-1)
B (n-1)
B (n-1) b (n-1)
Your task is to draw a fractal with a degree of N.
Input
The input contains several sets of test data. Each row of the data contains a positive integer not greater than 7 N, and the last line of the input is a negative number-1 indicates the end of the input.
Output
For each set of test data, the box fractal is output with an X-tick. Note that X is an uppercase letter. Outputs a hyphen of one row after each set of data output.
Ideas
Handle it recursively. Note that there is no more space at the end of the graph, so fill in the blanks with Fillchar.
Code
#include <stdio.h> #include <string.h>const int maxn = 1024;char Result[maxn][maxn];int pow3[maxn];void Fillchar (int n, int x, int y) {if (n = = 1) {Result[x][y] = ' x '; return; } int k = pow3[n-2]; int KK = k + k; for (int i = 0, i < K; i++) for (int j = 0; J < KK; J + +) Result[x + i][y + j] = "; for (int i = k, i < KK; i++) for (int j = 0; J < K; J + +) Result[x + i][y + j] = "; for (int i = KK; i < pow3[n-1]; i++) for (int j = 0; J < KK; J + +) Result[x + i][y + j] = "; Fillchar (n-1, x, y); Fillchar (n-1, x, y + kk); Fillchar (n-1, x + k, y + k); Fillchar (n-1, x + KK, y); Fillchar (n-1, x + KK, y + kk);} int main () {pow3[0] = 1; for (int i = 1; i < MAXN; i++) pow3[i] = pow3[i-1] * 3; int n; for (; scanf ("%d", &n), n! =-1;) {memset (result, 0, sizeof (result)); Fillchar (n, 0, 0); for (int i = 0; i < pow3[n-1]; i++) printf ("%s\n", Result[i]); printf ("-\n"); } RETUrn 0;}
POJ 2083 Fractal