Problem description | |
| |
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 3 4 -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 - 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 - 在做这道递归的题目时,如果简单得就是在5个位置上递归调用B(n-1)肯定不行,因为光标会落在前一个B(n-1)的最后,而不是第一行指定的位置,查阅了一些光标移动的函数,觉得太麻烦了。 后来想那就用一个大大的数组去保存图形吧,第一块直接调用B(n-1),然后画出中心的图形,最后用行对称和列对称去完成其余三个图形的绘制,对称的思想固然简单,但是严重超时了! 再后来,我想,那既然为了图快,那我就用一个三维数组,把7种图形都提前画好,到时候要哪个就去取哪个,速度就特别的快了。在画图的时候,用draw(x,y,n)在指定的(x,y)起点处画上第n-1 个图形,递归的思想就在每个图的绘制都是利用前一个图去画的(数组的复制memcpy(知道起点,知道终点,知道复制多少个字节即可))。画好以后,为了不输出多余的空格,还要对画好的图形 进行相应的处理(输出时可以按行一行一行地输出,所以在每行最后加上'\0',即字符串终结符,在找到最后一个X时一定要return,不然会一直i--到第一个X,于是我一开始总是只打印出左半部分 的图形!),代码如下,这个效率非常高:
|
算法学习10008(递归)
最新推荐文章于 2024-10-09 22:28:58 发布
本文详细探讨了递归算法的概念、工作原理及其实现细节,通过实例解析了递归在解决复杂问题中的应用。同时,讨论了递归算法的优缺点,包括其空间效率和时间复杂度,并提供了避免无限递归的策略。最后,文章介绍了如何将递归转化为迭代,以优化性能。
摘要由CSDN通过智能技术生成