C - Fractal(3.4.1)
来源:互联网 发布:mac mini 定制 编辑:程序博客网 时间:2024/05/29 19:48
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 :
Your task is to draw a box fractal of degree n.
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.
Sample Input
1234-1
Sample Output
X-X X XX X-X X X X X XX X X X X X X X XX X X X X XX X X X-X X X X X X X X X X X XX X X X X X X X X X X X X X X X X XX X X X X X X X X X X XX 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 XX X X X X X X X X X X XX X X X X X X X X X X X X X X X X XX X X X X X X X X X X XX X X X X X X X-
//递归方法#include <iostream>#include <string>#include <cstring>#include <cmath>using namespace std;#define M 1000char s[M][M];void print( int n, int x, int y){ int m;//m 表示方盒子规模 即边长 if( n == 1 ) { s[x][y] = 'X'; //回溯,当n=1时,为(x,y)坐标赋值'X' return ; } else if( n > 1 ) { m = pow( 3, n - 2 ); //计算盒子规模 print( n - 1, x, y ); //左上角第一个图形进行递归打印,第一个图形左上角坐标为(x,y) print( n - 1, x, y + 2 * m ); //右上角第二个图形进行递归打印,第二个图形左上角坐标为(x,y+2*m) print( n - 1, x + m, y + m ); //中间第三个图形进行递归打印,第三个图形左上角坐标为(x+m,y+m) print( n - 1, x + 2 * m, y ); //左下角第四个图形进行递归打印,第四个图形左下角坐标为(x+2*m,y) print( n - 1, x + 2 * m, y + 2 * m);//右下角第五个图形进行递归打印,第五个图形右下角坐标为(x+2*m,y+2*m) }}int main(){ int n, i, p; while(cin >> n && n != -1 ) { p = pow( 3, n - 1 ); //计算方盒子规模 for( i = 0; i < p; i++ ) //对数组进行初始化 { memset( s + i, ' ', p ); //每一行均初始化为空格 s[i][p] = '\n'; //每一行最后一列初始化为\0,如果不这样做,会出错,图形无法输出 } print( n, 0, 0 ); //从左上角(x,y)坐标开始打印 for( i = 0; i < p; i++) cout << s[i] ; //输出图形 cout << '-' << endl; } return 0;}
0 0
- C - Fractal(3.4.1)
- C - Fractal(3.4.1)
- C - Fractal(3.4.1)
- - Fractal(3.4.1)
- 《数据结构编程实验》 3.4.1Fractal
- Fractal
- Fractal
- Fractal
- (简单递归3.4.1)POJ 2083 Fractal(分形图的打印)
- CF 141 Div2 C Fractal Detector(状态压缩DP)
- Mandelbox Fractal
- poj2083 Fractal
- 【HIHOCODER】Fractal
- Fractal Interpolation
- JNOJ Fractal
- Fractal 分形学
- Ruby, Io, PHP, Python, Lua, Java, Perl, Applescript, TCL, ELisp, Javascript, OCaml, Ghostscript, and C Fractal Benchmark
- Codeforces Round #141 (Div. 2) C. Fractal Detector(神奇的状压DP)(好题)
- java中PropertyDescriptor的使用简介
- C++ - "No source available for "__chkstk_ms() at 0x401f0b"" 异常
- OC中的数组:NSArray
- UVa 1398 Meteor 解题报告(扫描线)
- 双系统重装Win7后,Ubuntu 无法启动的解决办法
- C - Fractal(3.4.1)
- 如何让 zend studio 10 识别 Phalcon语法并且进行语法提示
- C/C++ 开发库 | C/C++ Development Library
- lightoj 1140 How Many Zeroes? 数位dp
- poj 1426 dfs问题 01背包 创新性解法
- POJ1905 Expanding Rods
- 02--java中---注释、标识符、命名规范、数据类型
- HUDJ 2027 统计元音
- 0x000c0.200.200.0x000f5 0x000c0.0x000c8.200.245 0x000c0.0x000c8.0x000c8.0x000f6 0x000c0.0x000c8.0x00