SDKD 14级组队练习赛(一) J 递归打印图案

来源:互联网 发布:网络护卫怎么卡cf手游 编辑:程序博客网 时间:2024/05/19 21:43

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 Sierpinski fractal is defined as below:

  • A Sierpinski fractal of degree 1 is simply
    @
  • A Sierpinski fractal of degree 2 is
    @@@
  • If using B(n-1) to represent the Sierpinski fractal of degree n-1, then a Sierpinski fractal of degree n is defined recursively as following
    B(n-1)B(n-1)B(n-1)

    Your task is to draw a Sierpinski 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 10. The last line of input is an integer 0 indicating the end of input.

    Output

    For each test case, output the Sierpinski fractal using the '@' notation. Print a blank line after each test case.Don't output any trailing spaces at the end of each line, or you may get a PE!

    Sample Input

    120

    Sample Output

    @

    @
    @@

    瑞神的代码,自己太水了,刚学会递归。。。。
    #include<cstdio>#include<cstring>using namespace std;const int inf=0x3f3f3f3f;const int maxn=1030;char g[maxn][maxn];void draw(int n,int l1,int l2,int r1,int r2){    if(n==1){g[l1][r1]='@';return;}    int k=1<<(n-2);    draw(n-1,l1,l1+k,r1,r1+k);    draw(n-1,l2-k,l2,r1,r1+k);    draw(n-1,l2-k,l2,r2-k,r2);    return;}int main(){    int n;    while(~scanf("%d",&n)&&n){        memset(g,' ',sizeof(g));        draw(n,0,1<<(n-1),0,1<<(n-1));        int k=1<<(n-1);        for(int i=0;i<k;++i){            for(int j=k-1;j>=0;--j)                if(g[i][j]=='@') {g[i][j+1]=0;break;}            puts(g[i]);        }        puts("");    }    return 0;}


    0 0