递归作业
import java.util.Scanner;
/**
* @ Author : LHZ
* @ Data : Created in 21:04 2021/8/5
* @ Description: 递归作业
* @ Version : 1.8
*/
/*public class TestDemo {
*//**
* @ Author: LHZ
* @ Description: 递归函数
* @ DateTime: 21:19
* @ Params: n
* @ Return int
*//*
public static int mul(int n){
if(n==1){
return 1;
}
return n*mul(n-1);
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
System.out.println(mul(n));
}
}*/
/*
public class TestDemo {
*/
/**
* @ Author: LHZ
* @ Description: 求1+2+3+...10的和
* @ DateTime: 21:57
* @ Params: n
* @ Return int
*//*
public static int add(int n) {
if(n==1) {
return 1;
}
return n+add(n-1);
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
System.out.println(add(n));
}
}
*/
/*public class TestDemo {
*//**
* @ Author: LHZ
* @ Description: 整形按照数字顺序输出
* @ DateTime: 9:42
* @ Params: n
* @ Return void
*//*
public static void dis(int n) {
if(n>9) {
dis(n/10);
}
System.out.print(n%10+" ");
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int i=scan.nextInt();
dis(i);
}
}*/
/*
public class TestDemo {
*/
/**
* @ Author: LHZ
* @ Description: 整形按照数字反向顺序输出
* @ DateTime: 9:42
* @ Params: n
* @ Return void
*//*
public static void dis(int n) {
System.out.print(n%10+" ");
if(n>9) {
dis(n/10);
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int i=scan.nextInt();
dis(i);
}
}
*/
/*
public class TestDemo {
*/
/**
* @ Author: LHZ
* @ Description: 输出非负整数的各位数字相加的和
* @ DateTime: 10:01
* @ Params: n
* @ Return int
*//*
public static int sum(int n) {
if(n<9) {
return n%10;
}
return n%10+sum(n/10);
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int i=scan.nextInt();
System.out.println(sum(i));
}
}
*/
/*public class TestDemo {
*//**
* @ Author: LHZ
* @ Description: 求斐波那契的第n项
* @ DateTime: 10:57
* @ Params: n
* @ Return
*//* int
public static int sum(int n) {
if(n==1||n==2) {
return 1;
}
return sum(n-1)+sum(n-2);
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int i=scan.nextInt();
System.out.println(sum(i));
}
}
public class TestDemo {
*//**
* @ Author: LHZ
* @ Description: 打印
* @ DateTime: 10:57
* @ Params: n1,n2
* @ Return void
*//*
public static void move(char n1,char n3) {
System.out.print(n1+"->"+n3+" ");
}
*//**
* @ Author: LHZ
* @ Description: 汉诺塔函数
* @ DateTime: 14:08
* @ Params: n,n1,n2,n3
* @ Return void
*//*
public static void hnt(int n,char n1,char n2,char n3) {
if (n == 1) {
move(n1, n3);
} else {
hnt(n - 1, n1, n3, n2);
move(n1, n3);
hnt(n - 1, n2, n1, n3);
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
hnt(n,'a','b','c');
}
}*/
public class TestDemo {
/**
* @ Author: LHZ
* @ Description:青蛙跳台阶问题
* @ DateTime: 15:18
* @ Params: n
* @ Return int
*/
public static int qingWa(int n) {
if(n==1){
return 1;
}
if(n==2) {
return 2;
}
return qingWa(n-1)+qingWa(n-2);
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
System.out.println(qingWa(n));
}
}
结论:
递归问题不能深陷代码,需要横向思考。
当遇到递归问题时,首先应该找终止条件,然后在终止条件附近找规律。
规律即递归顺序。
难点:自身调用自身的转化。