初入算法篇 (递归)——poj2083

Fractal
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 10757 Accepted: 4836

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
-

题意:给出样例,让你打出n=1-7的图形

这题目已经明确暗示了用递归去做,大致思路是不会错的,要注意的是相隔距离m,我们可以发现

n=1  m=0

n=2  m=1

n=3  m=3

n=4 m=9

可得到m=3^(n-2)

第一个点(x,y)与中间的距离为(x+m,y+m) 同行右边的(x,y+2*m)那么我们可以写出五个点的递归表达式

    dfs(n-1,x,y);
    dfs(n-1,x,y+2*m);
    dfs(n-1,x+m,y+m);
    dfs(n-1,x+2*m,y);
    dfs(n-1,x+2*m,y+2*m);

边界条件为n==1,记得初始化,然后应该用%s输出,而不是用%c一个个输出,这样直接TLE

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string.h>
using namespace std;
char map[1000][1000];
void dfs(int n,int x,int y)
{
    int m;
    if(n==1)
    {
        map[x][y]='X';
        return ;//边界条件
    }
    m=pow(3.0,n-2);//左右上下相隔距离
    dfs(n-1,x,y);
    dfs(n-1,x,y+2*m);
    dfs(n-1,x+m,y+m);
    dfs(n-1,x+2*m,y);
    dfs(n-1,x+2*m,y+2*m);

}
int main()
{
    int t;
    int n,i,j;
    while(~scanf("%d",&n))
    {
        if(n==-1) break;
        int m=pow(3.0,n-1);
        for(i=1; i<=m; i++)
        {
            for(j=1; j<=m; j++)
            {
                map[i][j]=' ';//初始化
            }
            map[i][j]='\0';
        }
        dfs(n,1,1);
        for(i=1; i<=m; i++)
        {
            printf("%s",map[i]+1);
            printf("\n");
        }
        printf("-\n");
    }

}

在MATLAB中对数据进行多项式内插并绘制曲线图是数学建模和数据可视化的重要环节。《MATLAB曲线拟合与数据内插技术详解》将为你提供深入的技术解析和实战指导。 参考资源链接:[MATLAB曲线拟合与数据内插技术详解](https://wenku.csdn.net/doc/7rinsov8av?spm=1055.2569.3001.10343) 首先,你需要确保你的数据是散点形式,且已经导入MATLAB中。然后,使用MATLAB的interp1函数进行一维多项式内插。interp1函数能够根据一组已知的散点数据,估算出这些数据点之间的未知值。 假设你有一组一维散点数据x和y,你可以通过以下代码进行二次多项式内插: ```matlab x = [1, 2, 3, 4, 5]; % 已知的散点数据 y = [1, 4, 9, 16, 25]; % 已知的散点数据对应的函数值 % 使用interp1进行二次多项式内插 xx = linspace(min(x), max(x), 100); % 生成一个更密集的x轴数据点数组 yy = interp1(x, y, xx, 'poly', 2); % 'poly', 2指定二次多项式内插 % 绘制原始散点和内插后的平滑曲线 plot(x, y, 'o', xx, yy); % 使用'o'标记原始数据点 title('多项式内插曲线图'); xlabel('X轴'); ylabel('Y轴'); ``` 上述代码首先定义了原始散点数据x和y,然后使用interp1函数进行二次多项式内插,其中'poly', 2指定了多项式的阶数。最后,使用plot函数绘制出了原始散点以及通过内插得到的平滑曲线。 通过以上步骤,你可以在MATLAB中对散点数据进行多项式内插,并绘制出平滑的曲线图。为了进一步深入理解内插技术和曲线拟合,建议详细阅读《MATLAB曲线拟合与数据内插技术详解》,该资源不仅涵盖了基础概念,还包括高级技巧和案例研究,帮助你在数值分析和工程计算中达到新的高度。 参考资源链接:[MATLAB曲线拟合与数据内插技术详解](https://wenku.csdn.net/doc/7rinsov8av?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值