poj 2083 Fractal

Description

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 Input

1
2
3
4
-1

Sample Output

X
-
X X
 X
X X
-
X X   X X
 X     X
X X   X X
   X X
    X
   X X
X X   X X
 X     X
X X   X X
-
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
   X X               X X
    X                 X
   X X               X X
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
         X X   X X
          X     X
         X X   X X
            X X
             X
            X X
         X X   X X
          X     X
         X X   X X
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
   X X               X X
    X                 X
   X X               X X
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
-

Source

规律还是很好找的一层一层往下更新。横竖都是三倍递增。
 
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;
int n;
char s[2200][2200];
void sett(int a,int b)
{
    if(b > 7)
    {
        return;
    }
    for(int i = 0;i < a;i ++)
    {
        for(int j = 0;j < a;j ++)
        {
            s[i + a][j + a] = s[i + a * 2][j] = s[i][j + a * 2] = s[i + a * 2][j + a * 2] = s[i][j];
        }
    }
    sett(a * 3,b + 1);
}
int set3(int t)
{
    int d = 1;
    while(t)
    {
        d *= 3;
        t --;
    }
    return d;
}
int main()
{
    memset(s,' ',sizeof(s));
    s[0][0] = 'X';
    sett(1,2);
    while(~scanf("%d",&n) && n != -1)
    {
        int d = set3(n - 1);
        for(int i = 0;i < d;i ++)
        {
            for(int j = 0;j < d;j ++)
            {
                putchar(s[i][j]);
            }
            putchar('
');
        }
        printf("-
");
    }
}
View Code
原文地址:https://www.cnblogs.com/8023spz/p/9062094.html