什么是递归
程序调用自身的方法叫做递归
一般来讲,递归需要有边界条件,递归前进段和递归返回段.
当条件不满足时,递归前进,当递归条件满足时,递归返回.
- 递归的表现:函数调用函数自己
- 递归的用处:将大型复杂问题化解为若干小问题求解
- 递归的好处:代码量少
- 递归的弊端:占用空间,函数基于栈内存运行
分治算法举例
文件夹遍历
二分查找
有序数组种查找指定元素
求和1+2+3+…+(n-1)+n
package 第六章递归;
//求和
public class Demo {
public static void main(String[] args) {
System.out.println(f(100));
}
private static int f(int n) {
if(n==1) {
return 1;
}else {
return f(n-1)+n ;
}
}
}
求n的阶乘
package 第六章递归;
//阶乘
public class Demo {
public static void main(String[] args) {
System.out.println(f(100));
}
private static int f(int n) {
if(n==1||n==0) {
return 1;
}else {
return f(n-1)*n ;
}
}
}
斐波那契数列前20项:
1,1,2,3,5,8,13,21…
package 第六章递归;
public class Demo2 {
public static void main(String[] args) {
System.out.println(f(6));
}
private static int f(int n ) {
if(n==1||n==2) {
return 1;
}
return f(n-1)+f(n-2);
}
}
升级版
package 第六章递归;
public class Demo3 {
//3.斐波那契数列
public static void main(String[] args) {
//迭代方式
int n =1;
int m = 1;
int out=0;
for(int i =0;i<=50;i++) {
if(i==1||i==2) {
System.out.println(1+",");
}else {
out=n+m;
System.out.println(out+",");
n=m;
m=out;
}
}
//递归方式
/* for(int i =1;i<=50;i++) {
System.out.println(f(i));
}*/
}
public static int f(int n) {
if(n==1||n==2) {
return 1;
}
return f(n-1)+f(n-2);
}
}
汉诺塔问题
package 第六章递归;
public class Demo4 {
//汉诺塔问题
/*
前3个 x->z
前2个 x->y
前1个 x->z
第2个 x->y
前1个 z->y
第3个 x->z
前2个 y->z
前1个 y->x
第2个 y->z
前1个 x->z
========================
前64个 x->z 借助y
前63个 x->y 借助z
前62个 x->z 借助y
....
第63个 x->y
前62个 z->y 借助x
...
第64个 x->z
前63个 y->z 借助x
...
...
...
*/
public static void main(String[] args) {
move(64,"x","y","z");
}
private static void move(int i,String from,String mid,String to ) {
if(i==1) {
System.out.println(from+"->"+to);
}else {
move(i-1,from,to,mid);
System.out.println(from+"->"+to);
move(i-1,mid,from,to);
}
}
}
八皇后问题
package 第六章递归;
public class Demo6 {
//八皇后-N皇后问题
public static int count =0;
public static void main(String[] args) {
int[][] board = new int[8][8];
eightQueue(0,board);
}
private static void eightQueue(int row, int[][] board) {
if(row>=8) {
count++;
System.out.println("第"+count+"种情况");
for(int i=0;i<board.length;i++) {
for(int j=0;j<board[i].length;j++) {
System.out.print(board[i][j]+" ");
}
System.out.println();
}
}else {
//先复制一份棋盘
int[][] newBoard = new int[8][8];
for(int i =0;i<board.length;i++) {
for(int j=0;j<board[i].length;j++) {
newBoard[i][j]=board[i][j];
}
}
//在新棋盘上选棋
for(int col=0;col<8;col++) {
if(noDangerous(row,col,newBoard)) {
//放皇后前先清空当前行
for(int c=0;c<8;c++) {
newBoard[row][c]=0;
}
newBoard[row][col]=1;
eightQueue(row+1,newBoard);
}
}
}
}
private static boolean noDangerous(int row, int col, int[][] newBoard) {
//正上
for(int r = row-1;r>=0;r--) {
if(newBoard[r][col]==1) {
return false;
}
}
//左上
for(int r= row-1,c=col-1;r>=0&&c>=0;r--,c--) {
if(newBoard[r][c]==1) {
return false;
}
}
//右上
for(int r = row-1,c=col+1;r>=0&&c<8;r--,c++) {
if(newBoard[r][c]==1) {
return false;
}
}
return true;
}
}
数独问题
package 第六章递归;
import java.util.Scanner;
public class Demo07 {
public static int[][] board= new int[9][9];
public static void main(String[] args) {
//1.读取一个数独
Scanner scanner = new Scanner(System.in);
for(int i =0;i<9;i++) {
String line =scanner.nextLine();
for(int j=0;i<9;j++) {
board[i][j]=Integer.parseInt(line.charAt(j)+"");
}
}
//2.开始求解数独
solve(0,0);
}
public static void solve(int row,int col) {
if(row>=9) {
print();
System.exit(0);//程序立即结束
}else {
if(board[row][col]==0) {
for(int n =1;n<=9;n++) {
if(!isExist(row,col,n)) {
board[row][col]=n;
solve(row+(col+1)/9,(col+1)%9);
}
board[row][col]=0;
}
}else {
solve(row+(col+1)/9,(col+1)%9);
}
}
}
private static boolean isExist(int row,int col,int n) {
//先看行
for(int c=0;c<9;c++) {
if(board[row][c]==n) {
return true;
}
}
//再看列
for(int r =0;r<9;r++) {
if(board[r][col]==n) {
return true;
}
}
//后看小九宫
int rowMin=0;
int rowMax = 0;
int colMin=0;
int colMax=0;
//左上角 rowMin,colMin
//右下角rowMax,colMax
if(row>=0&&row<=2) {
rowMin=0;
rowMax=2;
}
if(row>=3&&row<=5) {
rowMin=3;
rowMax=5;
}
if(row>=6&&row<=8) {
rowMin=6;
rowMax=8;
}
if(col>=0&&col<=2) {
colMin=0;
colMax=2;
}
if(col>=3&&col<=5) {
colMin=3;
colMax=5;
}
if(col>=6&&col<=8) {
colMin=6;
colMax=8;
}
for(int r= rowMin;r<rowMax;r++) {
for(int c=colMin;c<colMax;c++) {
if(board[r][c]==n) {
return true;
}
}
}
return false;
}
private static void print() {
for(int i =0;i<9;i++) {
for(int j=0;i<9;j++) {
System.out.println(board[i][j]+" ");
}
System.out.println();
}
}
}
喜欢我的可以关注我,我们可以一起交流学习
微信公众号:
让我爱上它Computer
qq群:473989408