正文

让我郁闷一个下午的题2007-04-14 17:50:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/24887.html

分享到:

Fractal  
Problem 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  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. be careful, no extra blankspace allowed in the output.

 
Sample Input
1
2
-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

// 提交了几次,开始是超时,后来改进如下
// 可能不超时,但是Wrong Answer,测试怎么都没测出来
// 很多情况都考虑到了包括空格,回车,最后终于发现用管道测试时
// 如果输入文件里只有7(-1也在里面)得到的结果是乱码
// 不知道是不是错在这里。就算错了也不知道怎么改,我尝试了加大数组
// 甚至改为C++代码,结果仍是乱码,极度郁闷 !

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int       a[] = {1,3,9,27,81,243,729};
char   metrix[800][800];
 
void set(int x,int y,int n){
     metrix[x][y] = 'X';
     metrix[x-1][y-1] = 'X';
     metrix[x+1][y-1] = 'X';
     metrix[x+1][y+1] = 'X';
     metrix[x-1][y+1] = 'X';
     if(n == 2)
         return ;
     set(x, y, n - 1);
     set(x - a[n-2],y - a[n-2],n - 1);
     set(x + a[n-2],y - a[n-2],n - 1);
     set(x + a[n-2],y + a[n-2],n - 1);
     set(x - a[n-2],y + a[n-2],n - 1);
}

int main(){
    int  n,i,j,flag = 0;
    char saveIndex[800];

    memset(metrix,' ',800*800);
    set(364,364,7);
    while(1){
        scanf("%d",&n);
        if(n == -1)
            break;
        if(flag)
            printf("\n");
        if(n == 1){
            printf("X\n-");
            flag = 1;
            continue;
        }
        for(i = 0; i < a[n-1]; i++){
            for(j = a[n-1]; 1; j --){
                if(metrix[i][j] != ' ')
                    break;
            }
            saveIndex[i] = j+1;
            metrix[i][j+1] = '\0';
        }   
        for(i = 0; i < a[n-1]; i++)
            printf("%s\n",metrix[i]);
        for(i = 0; i < a[n-1]; i++)
            metrix[i][saveIndex[i]] = ' ';
        printf("-");
        flag = 1;
    }
    return 0;
}

阅读(2660) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册