代码有注释。
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef long long ll;
char s[1003][1003];
int mi(int n){ //计算3次幂
int ans=1;
for(int i=1;i<=n;i++)
{
ans*=3;
}
return ans;
}
void dfs(int n,int x,int y){
if(n==1){//边界
s[x][y]='X';
return;
}
int d=mi(n-2);
dfs(n-1,x,y);//对于每一个基础图形 都是这些位置有符号 这就是递归的精髓了
dfs(n-1,x,y+2*d);
dfs(n-1,x+d,y+d);
dfs(n-1,x+2*d,y);
dfs(n-1,x+2*d,y+2*d);
}
int main(){
int n;
while(scanf("%d",&n),n!=-1)
{
int d=mi(n-1);
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++){
s[i][j]=' ';//把输出的图先全变成空格 然后只需要修改这幅图里的元素
}
s[i][d]=' ';//末尾结束
}
dfs(n,0,0);
for(int i=0;i<d;i++){
printf("%s
",s[i]);
}
printf("-
");
}
}
Fractal
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11175 | Accepted: 4996 |
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 -