当前位置:   article > 正文

java学习----递归

java学习----递归

斐波那契数:

  1. public class Der {
  2. public static void main(String[] args) {
  3. tool nue=new tool();
  4. int a=nue.Fibonacci(6);
  5. System.out.println(a);
  6. }
  7. }
  8. class tool{
  9. public int Fibonacci(int n){
  10. if (n >= 1) {
  11. if (n == 2 || n == 1)
  12. return 1;
  13. else
  14. return Fibonacci(n-1)+Fibonacci(n-2);//斐波那契数从第三个开始就是前两位数之和为该位数
  15. }
  16. return 0;
  17. }
  18. }
  19. }
  1. //猴子吃桃,一天吃总数的一半多一个,第十天还有1个
  2. public class Der {
  3. public static void main(String[] args) {
  4. int day01=1;
  5. Monkey su=new Monkey();
  6. int v=su.Peach(day01);
  7. System.out.println(v);
  8. }
  9. }
  10. class Monkey{
  11. public int Peach(int n){
  12. if (n==10)//如果就是最后一天那么就时1个桃
  13. return 1;
  14. else if (n>=1&&n<=9)//十天这内
  15. return( Peach((n+1))+1)*2;//代表的是后一天(n+1),Peach(n+1)代表的是后一天的桃
  16. //( Peach((n+1))+1)*2前一天的桃=(后一天的桃+1)*2
  17. else
  18. return -1;
  19. }
  20. }

  1. //走迷宫
  2. public class Der {
  3. public static void main(String[] args) {
  4. int map[][] = new int[8][7];
  5. //布置地图
  6. for (int i = 0; i < 8; i++) {
  7. for (int j = 0; j < 7; j++) {
  8. map[0][j] = 1;
  9. map[i][0] = 1;
  10. map[7][j] = 1;
  11. map[i][6] = 1;
  12. }
  13. }
  14. map[1][2] = 1;
  15. map[3][1] = 1;
  16. for (int i = 0; i < 8; i++) {
  17. for (int j = 0; j < 7; j++)
  18. System.out.print(map[i][j]);
  19. System.out.println();
  20. }
  21. Secret su = new Secret();
  22. boolean v = su.Route(map, 1, 1);
  23. for (int i = 0; i < 8; i++) {
  24. for (int j = 0; j < 7; j++)
  25. System.out.print(map[i][j]);
  26. System.out.println();
  27. }
  28. }
  29. }
  30. class Secret{
  31. //设置0为可以走,1为障碍物,2为可以走且已走,3为走了但为死路
  32. public boolean Route(int a[][],int i,int j)
  33. {
  34. if (a[6][5]==2)//预设的终点,如果终点为2代表已经走到了。
  35. {
  36. return true;
  37. }else {
  38. if (a[i][j]==0)//如果当前的位置为可以走的点的话
  39. {
  40. a[i][j] = 2;//那么就将当前位置标记为2,为已走的位置
  41. if (Route(a, i + 1, j))//然后从当前位置向下一个走,判断是否可以走
  42. return true;
  43. else if (Route(a, i, j + 1))//再判断右边
  44. return true;
  45. else if (Route(a, i - 1, j))//上边
  46. return true;
  47. else if (Route(a, i, j - 1))//左边
  48. return true;
  49. else {
  50. a[i][j] = 3;//如果当前位置不可走则设置为3,走过为死路
  51. return false;
  52. }
  53. }
  54. else
  55. return false;
  56. }
  57. }
  58. }
  1. //汉诺塔
  2. public class Der {
  3. public static void main(String[] args) {
  4. tower hanoitower=new tower();
  5. hanoitower.Hanoitower(5,'A','B','C');
  6. }
  7. }
  8. class tower{
  9. //num表示需要移动卡片的个数
  10. //a,b,c表示移动的三根柱子
  11. public void Hanoitower(int num,char a,char b,char c){
  12. if (num==1)//如果个数为一个,则直接把a柱上的移动到c
  13. System.out.println(a+"->"+c);
  14. else {
  15. Hanoitower(num-1,a,c,b);//num-1表示总卡牌除了最下面的一个之和
  16. //要想把最后一个先移到c柱就得先移动前面的卡牌,那么就需要借助b达到目的
  17. System.out.println(a+"->"+c);//将a最后一个移到c
  18. Hanoitower(num-1,b,a,c);//b中移到a借助c
  19. }
  20. }
  21. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/845243
推荐阅读
相关标签
  

闽ICP备14008679号