Fractal
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 8356 | Accepted: 3972 |
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 :
Your task is to draw a box fractal of degree n.
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 -
题意:打印字符,很有意思的题目吧。
思路:先填满空格,最后递归打印就好了。
写的代码有点渣。。。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (2000+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
char str[MAXN][MAXN];
int Pow(int a, int b)
{
int ans = 1;
while(b--)
ans *= a;
return ans;
}
void getmap(int r1, int c1, int r2, int c2, int n)//在左上角(r1, c1) 右下角(r2, c2)的矩形区域内打印字符
{
if(n == 1)
{
str[r1][c1] = 'X';
return ;
}
int op = Pow(3, n-2);
getmap(r1, c1, r1+op-1, c1+op-1, n-1); getmap(r1, c1+op-1+op+1, r1+op-1, c1+op-1+op+1+op-1, n-1);
getmap(r1+op, c1+op, r1+op+op-1, c1+op+op-1, n-1);
getmap(r1+op-1+op+1, c1, r1+op-1+op+1+op-1, c1+op-1, n-1); getmap(r1+op+op, c1+op+op, r1+op+op+op-1, c1+op+op+op-1, n-1);
}
int main()
{
int n;
while( Ri(n), n != -1)
{
int M = Pow(3, n-1);
CLR(str, 0);
for(int i = 0; i < M; i++)
for(int j = 0; j < M; j++)
str[i][j] = ' ';
getmap(0, 0, M-1, M-1, n);
for(int i = 0; i < M; i++)
printf("%s\n", str[i]);
printf("-\n");
}
return 0;
}