POJ 2083 Fractal

Source: Internet
Author: User
Tags uppercase letter

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.