zoukankan      html  css  js  c++  java
  • poj1941(递归)

    The Sierpinski Fractal
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 4773   Accepted: 1979

    Description

    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.

    Input

    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.

    Output

    For each test case draw an outline of the Sierpinski Triangle with a side's total length of 2n characters. 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.

    Sample Input

    3
    2
    1
    0
    

    Sample Output

           /
          /__
         /  /
        /__/__
       /      /
      /__    /__
     /  /  /  /
    /__/__/__/__
    
       /
      /__
     /  /
    /__/__
    
     /
    /__
    

    分析:递归。开个二维数组,draw(i,j,N)表示在坐标(i,j)开始画三角形(边长2N),
    每次把大三角形分为三个小三角形,直到N==1的时候开始画。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    char s[2050][2050];
    
    void draw(int i,int j,int n)
    {
        if(n==1)
        {
            s[i][j]='/';
            s[i][j+1]='\';
            s[i+1][j-1]='/';
            s[i+1][j]='_';
            s[i+1][j+1]='_';
            s[i+1][j+2]='\';
        }
        else
        {
            int dis=(int)pow(2.0,n-1);
            draw(i,j,n-1);//大三角形分为三个小三角形 
            draw(i+dis,j-dis,n-1);
            draw(i+dis,j+dis,n-1);
        }
    }
    
    int main()
    {
        int N;
        while(scanf("%d",&N)&&N)
        {
            for(int i=0;i<2050;i++)
            memset(s[i],' ',sizeof(s[i]));
            int dis=(int)pow(2.0,N);
            draw(0,dis-1,N);
            for(int i=0;i<dis;i++)
            {
                int j=dis*2;
                while(j>=0)
                {
                    if(s[i][j]!=' ') break;
                    j--;
                }
                s[i][j+1]='';
                puts(s[i]);
            }
            printf("
    ");
        }
        return 0;
    }
    View Code



  • 相关阅读:
    jdk动态代理
    HTML+JavaScript实现在一个下拉框中多选,然后提交到另外一个下拉框中的效果
    Top中是如何取到Linux内核中的Hertz的?以及CPU使用率到底是怎么算出来的?
    C语言中的负数是如何表示的?
    Learning Python第二版笔记-Chapter 3 How to run your program
    Learning Python第二版笔记-Chapter 4 Numbers
    Learning Python第二版笔记-Chapter 1 & 2
    Firefox中的document.all的替代方案From EasyCluster support Firefox
    Linux下共享库中的全局变量,静态变量是否只有一份?
    C和C++编程中static关键字的含义-修饰函数和变量
  • 原文地址:https://www.cnblogs.com/ACRykl/p/8325571.html
Copyright © 2011-2022 走看看