1.什么是递归:
递归就是在一个方法中反复调用自身,从而达到某种目的的功能。
比如:
public void count(int n,int i)
{
n+=i;
if(i==0)
System.out.printfln(n);
else count(n,i--);
}
2.递归所需的必要内容:
- 终止条件,当方法满足某个特定条件时,进行值的返回或者输出,不再调用自身。
- 递归调用,每经过一次递归,方法就越接近终止条件。
3.样例:
-汉诺塔问题:
输入一个正整数n,表示有n个盘片在第一根柱子上。输出操作序列,格式为“x–>y”。每个操作一行,表示把x柱子上的编号为t的盘片挪到柱子y上。柱子编号为A,B,C,你要用最少的操作把所有的盘子从A柱子上转移到C柱子上。
public han()
{
public void move( char x, char y){x+=65;y+=65;
System.out.printfln(x+"-->"+y);
}
public void f(int n,int A,int B,int C)
{
if(n==1)
{
move(A,C);
}
else
{
f(n-1,A,C,B);
move(A,C);
f(n-1,B,A,C);
}
}
public static void main(String args[])
{
han ha=new han();
int n=10;
ha.f (n,0,1,2);
}
}
-门格海绵
它是康托尔集和谢尔宾斯基地毯在三维空间的推广
构造特点
1.画出一个立方体
2.将它等分三层(底层,中层,顶层)即等分27个完全相同的小立方体
3.掏空每一面中央的小立方体,包括最内层的小立方体
4.对剩余的20个小立方体都重复1~3的操作,并渐变填充每个小立方体的三面,使其更有立体感
import java.awt.Graphics;
import javax.swing.JFrame;
public class dUI {
Graphics g;
public static void main(String args[])
{
dUI fu=new dUI();
fu.show();
}
public void show()
{
JFrame jf=new JFrame("分形递归");
mol ml=new mol();
jf.addMouseListener(ml);
jf.setSize(1000, 1000);
jf.setVisible(true);
g=jf.getGraphics();
ml.g=g;
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class mol implements MouseListener{
Graphics g;
public void mouseClicked(MouseEvent e)
{
paint(4,300,300,270);
}
/**
* Invoked when a mouse button has been pressed on a component.
*/
public void mousePressed(MouseEvent e)
{
}
/**
* Invoked when a mouse button has been released on a component.
*/
public void mouseReleased(MouseEvent e)
{
}
/**
* Invoked when the mouse enters a component.
*/
public void mouseEntered(MouseEvent e)
{
}
/**
* Invoked when the mouse exits a component.
*/
public void mouseExited(MouseEvent e)
{
}
public void RECT(int x1,int y1,int wide)
{
g.setColor(new Color(135,206,250));
g.fillRect(x1, y1, wide, wide);
int x2,y2,x3,y3,x4,y4,x5,y5,x6,y6;
x2=x1+wide/2;x3=x2+wide;x4=x3;x5=x1+wide;x6=x5;
y2=y1-wide/2;y3=y2;y4=y3+wide;y5=y1;y6=y5+wide;
int[] xa={x1,x2,x3,x5};
int[] ya={y1,y2,y3,y5};
g.setColor(new Color(135,230,250));
g.fillPolygon(xa, ya, 4);
int[] xb={x5,x3,x4,x6};
int[] yb={y5,y3,y4,y6};
g.setColor(new Color(135,180,250));
g.fillPolygon(xb, yb, 4);
}
// 递归用
public void paint(int n,int x,int y,int wide)
{
if(n==1)
{
RECT(x,y,wide);
return;
}
//对大立方体进行分割
int[] x1=new int[20]; int[] y1=new int[20];
x1[5]=x; y1[5]=y;
x1[6]=x1[5]+wide/3; y1[6]=y1[5];
x1[7]=x1[5]+2*wide/3; y1[7]=y1[5];
x1[3]=x+wide/6; y1[3]=y1[5]-wide/6;
x1[4]=x1[3]+2*wide/3; y1[4]=y1[3];
x1[0]=x1[5]+wide/3; y1[0]=y1[5]-wide/3;
x1[1]=x1[0]+wide/3; y1[1]=y1[0];
x1[2]=x1[0]+2*wide/3; y1[2]=y1[0];
x1[10]=x1[5]; y1[10]=y1[5]+wide/3;
x1[11]=x1[7]; y1[11]=y1[10];
x1[8]=x1[0]; y1[8]=y1[0]+wide/3;
x1[9]=x1[2]; y1[9]=y1[8];
x1[17]=x; y1[17]=y1[5]+2*wide/3;
x1[18]=x1[17]+wide/3; y1[18]=y1[17];
x1[19]=x1[17]+2*wide/3; y1[19]=y1[17];
x1[15]=x+wide/6; y1[15]=y1[3]+2*wide/3;
x1[16]=x1[15]+2*wide/3; y1[16]=y1[15];
x1[12]=x1[15]+wide/6; y1[12]=y1[0]+2*wide/3;
x1[13]=x1[12]+wide/3; y1[13]=y1[12];
x1[14]=x1[12]+2*wide/3; y1[14]=y1[12];
for(int i=12;i<20;i++)
{
paint(n-1,x1[i],y1[i],wide/3);
}
for(int i=8;i<12;i++)
{
paint(n-1,x1[i],y1[i],wide/3);
}
for(int i=0;i<8;i++)
{
paint(n-1,x1[i],y1[i],wide/3);
}
}
}
To be continue…