总时间限制: 1000ms 内存限制: 65536kB

描述

Consider a regular triangular area, divide it into four equal triangles of half height and remove the one in the middle. Apply the same operation recursively to each of the three remaining triangles. If we repeated this procedure infinite times, we'd obtain something with an area of zero. The fractal that evolves this way is called the Sierpinski Triangle. Although its topological dimension is 2, its Hausdorff-Besicovitch dimension is log(3)/log(2)~1.58, a fractional value (that's why it is called a fractal). By the way, the Hausdorff-Besicovitch dimension of the Norwegian coast is approximately 1.52, its topological dimension being 1.

For this problem, you are to outline the Sierpinski Triangle up to a certain recursion depth, using just ASCII characters. Since the drawing resolution is thus fixed, you'll need to grow the picture appropriately. Draw the smallest triangle (that is not divided any further) with two slashes, to backslashes and two underscores like this:

 /\
/__\

To see how to draw larger triangles, take a look at the sample output.

输入

The input contains several testcases. Each is specified by an integer n. Input is terminated by n=0. Otherwise 1<=n<=10 indicates the recursion depth.

输出

For each test case draw an outline of the Sierpinski Triangle with a side's total length of 2ncharacters. Align your output to the left, that is, print the bottom leftmost slash into the first column. The output must not contain any trailing blanks. Print an empty line after each test case.

样例输入


样例输出

       /\
/__\
/\ /\
/__\/__\
/\ /\
/__\ /__\
/\ /\ /\ /\
/__\/__\/__\/__\ /\
/__\
/\ /\
/__\/__\ /\
/__\

解题思路

一开始总是以小三角形为单位,百思不得其解。看了一眼网上的思路之后恍然大悟要用数组做,递归的整体过程也写的很轻松,然后被各种换行空字符bug折磨,调了一个多小时orz。

AC代码

#include<iostream>
#include<cstring>
using namespace std; char map[][];//x向上延展是行,y向右延展是列 void GetMap(int t, int x, int y)//x,y是每个三角形的起点
{
if (t == )
{
map[x][y] = '/', map[x][y+] = '_', map[x][y+] = '_', map[x][y+] = '\\';
map[x + ][y] = ' ', map[x + ][y + ] = '/', map[x + ][y + ] = '\\';
}
else
{
GetMap(t - , x, y);
GetMap(t - , x, ( << t) + y);
GetMap(t - , ( << (t - )) + x, ( << (t - )) + y);
}
} void Draw(int t)
{
int m = ( << t) + ;
for (int i = <<t; i > ; i--)
{
for (int j = ; j <= m; j++)
{
if (map[i][j])
{
cout << map[i][j];
}
else cout << ' ';
}
cout << endl;
m++;
}
} int main()
{
int t;
int num = ;
while (true)
{
cin >> t;
if (t == )break;
GetMap(t,,);
if(num > ) cout << endl;
num++;
Draw(t);
}
//system("pause");
return ;
}

POJ 1941 The Sierpinski Fractal的更多相关文章

  1. poj 1941 The Sierpinski Fractal 递归

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

  2. POJ 1941 The Sierpinski Fractal ——模拟

    只需要开一个数组,记录一下这个图形. 通过一番计算,发现最大的面积大约是2k*2k的 然后递归下去染三角形. 需要计算出左上角的坐标. 然后输出的时候需要记录一下每一行最远延伸的地方,防止行末空格过多 ...

  3. POJ1941 The Sierpinski Fractal

    Description Consider a regular triangular area, divide it into four equal triangles of half height a ...

  4. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  5. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  6. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  7. poj 题目分类(1)

    poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...

  8. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  9. POJ题目分类(转)

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

随机推荐

  1. lis框架各种方法的使用

    //这个必须是lpedorapp表的主键才行LPEdorAppDB tLPEdorAppDB = new LPEdorAppDB();tLPEdorAppDB.setEdorAcceptNo(mEdo ...

  2. JS优化常用片断

    防抖debounce装饰器 在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时. function debounce(func, delay) { let isCooldown = fa ...

  3. learning java 实例序列化

    对Person类实例进行序例化及反序例化: Person.java public class Person implements java.io.Serializable { private Stri ...

  4. GitHub页面基本知识

    官网地址:https://help.github.com/categories/github-pages-basics/ GitHub页面是什么? GitHub页面是一个静态的站点托管服务. GitH ...

  5. Android程序员问答题

    前言 最近三个月内,不断地进行移动应用开发在线测试题,也积累了不一样的知识.这也将对android studio有很好的掌握,对将来面试也很有好处.那么我就分享给大家.分享是一种幸福,这是一种质的飞越 ...

  6. 02-线性结构3 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  7. JAVA中Stringbuffer的append( )方法

    Stringbuffer是动态字符串数组,append( )是往动态字符串数组添加,跟“xxxx”+“yyyy”相当‘+’号. 跟String不同的是Stringbuffer是放一起的,String1 ...

  8. uiautomator2使用教程

    一.要求 python 3.6+ android 4.4+   二.介绍 uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库.其底层基于Google uiaut ...

  9. centos7下配置ftp服务器

    第一步,安装vsftpd这款ftp服务器软件,yum install -y vsftpd 第二步,设置vsftpd服务开机自启动,然后重启服务,查看ftp服务端口,centos6命令如下: #chkc ...

  10. ssh修改默认远程端口

    ---------------------centos6-----------------1.查看系统版本cat /etc/redhot-releose 2.编辑sshd配置,修改默认的端口vim / ...