Fractal
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8341   Accepted: 3965

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
  • 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 OutputX

-
X X
X
X X
-
X X X X
X X
X X X X
X X
X
X X
X X X X
X X
X X X X
-
X X X X X X X X
X X X X
X X X X X X X X
X X X X
X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
X X X X
X X
X X X X
X X
X
X X
X X X X
X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
X X X X
X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
-
递归:
思路很巧妙,把每个左上角的点作为起点;开始递归;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x);
char a[2500][2500];
void dfs(int x,int y,int cur){
if(cur==1){
a[x][y]='X';
return;
}
int n,m;
dfs(x,y,cur-1);
//左上
m=x;n=y+pow(3,cur-2)*2;
dfs(m,n,cur-1);
//右上
m=x+pow(3,cur-2)*2;n=y;
dfs(m,n,cur-1);
//左下
m=x+pow(3,cur-2)*2;n=y+pow(3,cur-2)*2;
dfs(m,n,cur-1);
//右下
m=x+pow(3,cur-2);n=y+pow(3,cur-2);
dfs(m,n,cur-1);
//中
}
int main(){
int N;
while(scanf("%d",&N),N!=-1){
int len=pow(3,N-1);
for(int i=0;i<len;i++){
for(int j=0;j<len;j++)
a[i][j]=' ';
a[i][len]='\0';
}
dfs(0,0,N);
for(int i=0;i<len;i++){
printf("%s\n",a[i]);
}
puts("-");
}
return 0;
}

  

 

Fractal(递归,好题)的更多相关文章

  1. 递归一题总结(OJ P1117倒牛奶)

    题目:                    农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另 ...

  2. 函数递归简单题-hdoj-2044 2018-一只小蜜蜂 母牛的故事

    题目:一只小蜜蜂 递归做法: #include<cstdio> #include<iostream> #include<stdlib.h> #include< ...

  3. poj 2083 Fractal 递归 图形打印

    题目链接: http://poj.org/problem?id=2083 题目描述: n = 1时,图形b[1]是X n = 2时,图形b[2]是X  X        X               ...

  4. 洛谷P1427 小鱼的数字游戏 题解 递归入门题

    题目链接:https://www.luogu.com.cn/problem/P1427 题目大意: 给你一串数(输入到0为止),倒序输出这些数. 解题思路: 首先这道题目可以用数组存数据,然后输出. ...

  5. 十五 链表与递归,leetCode203题

    两种方式: package com.lt.datastructure.LinkedList; /** * leetCode 203题 * /** * Definition for singly-lin ...

  6. 一道Postgresql递归树题

    转载请注明出处: https://www.cnblogs.com/funnyzpc/p/13698249.html 也是偶然的一次,群友出了一道题考考大家,当时正值疫情最最严重的三月(借口...),披 ...

  7. poj 1941 The Sierpinski Fractal 递归

    //poj 1941 //sep9 #include <iostream> using namespace std; const int maxW=2048; const int maxH ...

  8. HDU 2563 统计问题(递归,思维题)

    统计问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. POJ 2083 Fractal 分形题目

    这两天自学了一线算法导论里分治策略的内容,秉着只有真正投入投入编程,才能更好的理解一种算法的思想的想法,兴致勃勃地找一些入门的题来学习. 搜了一下最后把目光锁定在了Poj fractal这一个题上.以 ...

随机推荐

  1. MQ学习(二)----ActiveMQ简介(转)

    1.  什么是ActiveMQ ActiveMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信.ActiveMQ使用A ...

  2. java下拉框,滚动条

    package com.soft.test; /** * 下拉列表.下拉框.滚动条的使用 */ import javax.swing.*; import java.awt.*; public clas ...

  3. 投资新兴市场和细分市场 good

    新兴市场对程序员来说,就是一种新的语言.一个新的平台.一套新的框架.新兴市场因为刚刚兴起,所以几乎所有人都在同一个起跑线,特别适合后进者.我认识从一个2011年开始学习iOS开发的同学,他能能力中等, ...

  4. 调色板类QPalette——包含了Qt窗口不见的颜色组(collor group),和Windows右键属性外观非常类似

    QPalette类包含了Qt窗口不见的颜色组(collor group); 1.Active组,该组的颜色用户当前活动的(active)窗口,即具有键盘或鼠标焦点的窗口; 2.Inactive组,该组 ...

  5. Android SDK与API版本的对应关系

    看教程.开发Android程序等很多地方,需要设置Android SDK的版本,而其要我们写的却是API版本的数字, 为了方便查看 Android SDK与API版本的对应关系 我在SDK Manag ...

  6. 关于crontab笔记

    如下所示,一般crontab文件里面的定时任务格式如下所示: 59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2 ...

  7. 自增字段 auto_commit的研究分析

    MySQL自增字段,自增字段计数器在主存储里面,不在硬盘上(This counter is stored only in main memory, not on disk). 1,添加表,设立自增主键 ...

  8. 使用netty构建一个socks proxy

    使用netty构建一个socks proxy   最近在做的项目,需要自己搭建一个socks代理.netty4.0附带了一个socks代理的样例,但是3.x就没有这个东西了,碰巧使用的又是3.7,就只 ...

  9. android下tcpdump抓包

    tcpdump是最快捷方便的抓包方式,还可以加深对网络协议的理解.android下可以通过如下方式抓包: 1 Android上启动tcpdump Android设备可以把tcpdump的可执行文件上传 ...

  10. Windows下如何建立以"."开头的文件夹

    Windows资源管理器不允许创建点开头的文件或文件夹,但在cmd命令提示符下是可以的: 创建命令: md d:\.myfolder .myfolder就是以点开头的文件夹的名称