zoukankan      html  css  js  c++  java
  • C

    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.

    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
    -

    对这样的递归图形问题,只是简单的将其写出函数即找出规律,然后找出边界,然后写成递归就行了

    有点逗比的是将题目中的-写成了_,wr了好多次!!!!    

    本题使用函数pow(a,b)即a的b次方

    #include <iostream>
    #include <cstdio>
    #include <memory.h>
    #include <cmath>
    using namespace std;
    char map[1000][1000];
    void paint(int n,int x,int y){
     if(n==1){
      map[x][y]='X';
      return;
     }
     int size = (int)pow(3.0,n-2);    
     paint(n-1, x, y);      //左上角
     paint(n-1, x, y+size*2);   //右上角
     paint(n-1, x+size, y+size);         //中間
     paint(n-1, x+size*2, y);            //左下角
     paint(n-1, x+size*2, y+size*2); //右下角
    }
    int main(){
    	int n;
    	while (~scanf("%d",&n)&&n!=-1){
    	  int size=(int)pow(3.0, n-1);       //度为n的分形图的规模是3^(n-1)
    	  memset(map,' ',sizeof(map));
    	  paint(n, 1, 1);
    	  for (int i=1;i<=size;i++ )   {   //列印         
    	    for(int j=1;j<=size;j++)
    	     cout<<map[i][j];
    	    cout<<endl;         
    	  }
    	   cout<<'-'<<endl;
    	}
     return 0;
    }


  • 相关阅读:
    Spark Streaming 中管理 Kafka Offsets 的几种方式
    Kafka 在华泰证券的探索与实践
    Kafka 客户端是如何找到 leader 分区的
    关于Java中如何判断字符串是否为json格式
    关于JAVA递归遍历树级菜单结构
    关于JDK8 三种时间获取方法以及日期加减
    关于在Java里面,时间的一些业务
    关于Java的IO通信
    关于git同时提交到2个仓库gitee github
    关于JVM——工具(二)
  • 原文地址:https://www.cnblogs.com/zswbky/p/5432138.html
Copyright © 2011-2022 走看看