1、方法定义中调用方法本身的现象
2、递归注意实现
1) 要有出口,否则就是死递归
2) 次数不能太多,否则就内存溢出
3) 构造方法不能递归使用
3、递归解决问题的思想和图解:

分解和合并【先分解后合并】

1. 常见的斐波那契数列

1,1,2,3,5,8,13,21,...
特征: 从第三个数开始,每个数是前两个数的和。

int count = 0;

    private int getFibo(int i) {

        if (i == 1 || i == 2) {
count = count+1;
System.out.println("第" +count+"次进行运算 并返回结果1" );
return 1;}
else
{
count = count+1;
System.out.println("第" +count+"次进行运算 "+ "getFibo("+(i - 1)+")"+" + getFibo("+(i - 2)+")");
return getFibo(i - 1) + getFibo(i - 2);
}
} @Test
public void test01() {
int value = getFibo(6);
System.out.println(value);
}

 

 

2. 阶乘
10!= 10 * 9 * 8 * 7 * (... )* 1
9! = 9 * 8 * 7 * (... )* 1
8! = 8 * 7 * (... )* 1
特征:
9!=9* 8!
10! =10 * 9!

//阶乘
private int get(int i){
int result = 1;
if (i == 1) {
count = count+1;
System.out.println("第" +count+"次进行运算 并返回结果* 1" );
result = result * 1;
}
else {
count = count+1;
System.out.println("第" +count+"次进行运算" + "get(" +(i-1)+")" );
result = i * get(i-1);
}
return result; } @Test
public void test01() {
//System.out.println(getFibo(6));
System.out.println(get(5));
}

  

3. 加法实现1+2+3+4+5+...+100=

 //求和
private int fsum(int i){ if (i <= 0) {
count = count+1;
System.out.println("第" +count+"次进行运算并返回0" );
return 0;
}
else {
count = count+1;
System.out.println("第" +count+"次进行运算且返回 " + i +" + fsum(" +(i-1)+")" );
return (i + fsum(i-1)); } } @Test
public void test01() {
//System.out.println(getFibo(6));
//System.out.println(get(5));
System.out.println(fsum(10)); }

  

4. 实现打印乘法表

//打印乘法表
//for 循环实现
private void getByFor(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i+" * "+j+" = "+i*j+" ");
}
System.out.println();
}
}
//打印乘法表
//递归实现
public static void getByRecursion(int n) {//递归 实现
if (n == 1) {
System.out.println("1 * 1 = 1 ");
}
else {
getByRecursion(n-1);
for (int j = 1; j <= n; j++) {
System.out.print(n+" * "+j+" = "+n*j+" ");
}
System.out.println();
}
} @Test
public void test01() {
//System.out.println(getFibo(6));
//System.out.println(get(5));
//System.out.println(fsum(10)); getByFor(8);
getByRecursion(9); }

  

6. 汉诺塔游戏

三根木棒,n个依次增大的空心圈圈,每次移动一个圈圈到木棒上,且任何时候保证小的圈圈不能被大的圈圈压在下面。

2的n次方-1

//5. 汉诺塔(又称河内塔)问题其实是印度的一个古老的传说
public int hanio(int n,char a,char b,char c) {
if (n == 1) {
System.out.println( n + "号盘子从" + a + "到" + c);
count = count+1;
return count; } else {
count = count+1;
hanio(n - 1, a, c, b);//把上面n-1个盘子从a借助b搬到c
System.out.println("移动" + n + "号盘子从" + a + "到" + c);//紧接着直接把n搬动c
hanio(n - 1, b, a, c);//再把b上的n-1个盘子借助a搬到c
return count;
} } @Test
public void test01() {
//System.out.println(getFibo(6));
//System.out.println(get(5));
//System.out.println(fsum(10)); //getByFor(8);
//getByRecursion(9); int count =hanio(3,'A','B','C');
System.out.println(count); }

  

代码:

package com.example.demo;

import org.junit.Test;

public class Test02 {

    int count = 0;

    //1. 斐波那契数列递归,用的时候请将count和输出System.Out去除
private int getFibo(int i) { if (i == 1 || i == 2) {
count = count+1;
System.out.println("第" +count+"次进行运算 并返回结果1" );
return 1;}
else
{
count = count+1;
System.out.println("第" +count+"次进行运算 "+ "getFibo("+(i - 1)+")"+" + getFibo("+(i - 2)+")");
return getFibo(i - 1) + getFibo(i - 2);
}
} //2. 阶乘
private int get(int i){
int result = 1;
if (i == 1) {
count = count+1;
System.out.println("第" +count+"次进行运算并返回result * 1" );
result = result * 1;
}
else {
count = count+1;
System.out.println("第" +count+"次进行运算且返回 " + i+" * get(" +(i-1)+")" );
result = i * get(i-1);
}
return result; } //3. 求和
private int fsum(int i){ if (i <= 0) {
count = count+1;
System.out.println("第" +count+"次进行运算并返回0" );
return 0;
}
else {
count = count+1;
System.out.println("第" +count+"次进行运算且返回 " + i +" + fsum(" +(i-1)+")" );
return (i + fsum(i-1)); }
} //打印乘法表
//for 循环实现
private void getByFor(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i+" * "+j+" = "+i*j+" ");
}
System.out.println();
}
}
//打印乘法表
//4. 递归实现
public void getByRecursion(int n) {//递归 实现
if (n == 1) {
System.out.println("1 * 1 = 1 ");
}
else {
getByRecursion(n-1);
for (int j = 1; j <= n; j++) {
System.out.print(n+" * "+j+" = "+n*j+" ");
}
System.out.println();
}
} //5. 汉诺塔(又称河内塔)问题其实是印度的一个古老的传说
public int hanio(int n,char a,char b,char c) {
if (n == 1) {
System.out.println( n + "号盘子从" + a + "到" + c);
count = count+1;
return count; } else {
count = count+1;
hanio(n - 1, a, c, b);//把上面n-1个盘子从a借助b搬到c
System.out.println("移动" + n + "号盘子从" + a + "到" + c);//紧接着直接把n搬动c
hanio(n - 1, b, a, c);//再把b上的n-1个盘子借助a搬到c
return count;
} } @Test
public void test01() {
//System.out.println(getFibo(6));
//System.out.println(get(5));
//System.out.println(fsum(10)); //getByFor(8);
//getByRecursion(9); int count =hanio(3,'A','B','C');
System.out.println(count); } }

  

package com.example.demo;

import org.junit.Test;

public class Test03 {

    int count = 0;

    //1. 斐波那契数列递归,用的时候请将count和输出System.Out去除
private int getFibo(int i) { if (i == 1 || i == 2) {
return 1;}
else
{
return getFibo(i - 1) + getFibo(i - 2);
}
} //2. 阶乘
private int get(int i){
int result = 1;
if (i == 1) {
result = result * 1;
}
else {
result = i * get(i-1);
}
return result; } //3. 求和
private int fsum(int i){ if (i <= 0) {
return 0;
}
else {
return (i + fsum(i-1)); }
} //打印乘法表
//for 循环实现
private void getByFor(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i+" * "+j+" = "+i*j+" ");
}
System.out.println();
}
}
//打印乘法表
//4. 递归实现
public void getByRecursion(int n) {//递归 实现
if (n == 1) {
System.out.println("1 * 1 = 1 ");
}
else {
getByRecursion(n-1);
for (int j = 1; j <= n; j++) {
System.out.print(n+" * "+j+" = "+n*j+" ");
}
System.out.println();
}
} //5. 汉诺塔(又称河内塔)问题其实是印度的一个古老的传说
public int hanio(int n,char a,char b,char c) {
if (n == 1) {
System.out.println( n + "号盘子从" + a + "到" + c);
count = count+1;
return count; } else {
count = count+1;
hanio(n - 1, a, c, b);//把上面n-1个盘子从a借助b搬到c
System.out.println("移动" + n + "号盘子从" + a + "到" + c);//紧接着直接把n搬动c
hanio(n - 1, b, a, c);//再把b上的n-1个盘子借助a搬到c
return count;
} } @Test
public void test01() {
System.out.println(getFibo(6));
System.out.println(get(5));
System.out.println(fsum(10)); getByFor(8);
getByRecursion(8); int count =hanio(3,'A','B','C');
System.out.println(count); } }

  

面试中常见的算法之Java中的递归的更多相关文章

  1. 常见排序算法(附java代码)

    常见排序算法与java实现 一.选择排序(SelectSort) 基本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置进行交换:接着对不包括第一个记录以外的其他 ...

  2. 常见排序算法总结 -- java实现

    常见排序算法总结 -- java实现 排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序. 线性时间 ...

  3. 正整数构成的线性表存放在单链表中,编写算法将表中的所有的奇数删除。(C语言)

    /* 正整数构成的线性表存放在单链表中,编写算法将表中的所有的奇数删除 */ #include <stdio.h> #include <stdlib.h> typedef st ...

  4. iOS面试中常见的算法题目

    一.前言 这里是在iOS求职中自己遇到的算法题,希望对大家有所帮助.不定期更新.如果大家想在线运行代码调试,可以将代码拷贝到这里.然后进行调试.下面就是常见的算法题目. 二.正文 1.就n的阶乘.(这 ...

  5. Java开发中常见的危险信号(中)

    本文来源于我在InfoQ中文站原创的文章,原文地址是:http://www.infoq.com/cn/news/2013/12/common-red-flags-in-java-1 Dustin Ma ...

  6. 最近在准备面试,总结了几个java中面向对象的几个问题,问题本事还不够全面,要想知道还是要自己去找,但是在面试上应该是没多大问题了

    Overload(重载)与Override(重写)的区别 重载:发生在一个类中,方法名称相同,参数列表不同,方法体不同(看对象类型) 重写:发生在父类中,方法名称相同,参数列表相同,方法体不同(看引用 ...

  7. 几种常见排序算法的java实现

    一.几种常见的排序算法性能比較 排序算法 最好时间 平均时间 最坏时间 辅助内存 稳定性 备注 简单选择排序 O(n^2) O(n^2) O(n^2) O(1) 不稳定 n小时较好 直接插入排序 O( ...

  8. java中接口的简单运用&java中的一些异常(运用myeclipse)

    package test;//创建一个名为test的包 public class A4paper implements Paper { public String getSize(){ return& ...

  9. .NET和JAVA中BYTE的区别以及JAVA中“DES/CBC/PKCS5PADDING” 加密解密在.NET中的实现

    场景:java 作为客户端调用已有的一个.net写的server的webservice,输入string,返回字节数组. 问题:返回的值不是自己想要的,跟.net客户端直接调用总是有差距 分析:平台不 ...

随机推荐

  1. 【wpf】在win10系统上弹出toast和notification

    原文:[wpf]在win10系统上弹出toast和notification 老规矩,先看效果 右下角的notification: 操作中心的notification: 整体效果: 前提条件 1.需要在 ...

  2. WPF属性(一)依赖属性

    原文:WPF属性(一)依赖属性 依赖属性是一种可以自己没有值,并能通过使用Binding从数据源获得值的属性,拥有依赖属性的对象称为依赖对象,在传统开发中,一个对象所占用的内存在调用new操作符进行实 ...

  3. C/C++网络编程时注意的问题小结

    1.网络编程在自己定义结构体实现协议的时候,一定要注意字节对齐这个问题.否则sizeof和强制转换指针的时候都会出现很难发现的bug. 什么是字节对齐自行百度. #pragma pack (1)//字 ...

  4. SQL Server 限制IP登陆(登陆触发器运用)

    原文:SQL Server 限制IP登陆(登陆触发器运用) 一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 补 ...

  5. NAudio的使用说明

    官方网站:http://naudio.codeplex.com/ 源码:https://github.com/naudio/NAudio NuGet安装: Install-Package NAudio ...

  6. 解决手机提示TF卡受损需要格式化问题

    昨晚因为上QQ FOR PAD后.关机.结果又杯具了.上次无意看到一个SD卡修复命令,收藏起来了.一试,还真管用.现把它写出来.分享给大家.以后出现SD卡受损,千万不要再格式化内存卡了.修复过程:1. ...

  7. MIPS虚拟机代码

    http://download.eeworld.com.cn/download/mamselc/472333http://download.eeworld.com.cn/detail/lamas/36 ...

  8. 无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间的排序规则冲突。

    无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间的排序规则冲突 ...

  9. 企业级架构 MVVM 模式指南 (WPF 和 Silverlight 实现) 译(3)

    第一章 表现模式关注分离(soc)是企业及软件开发中非常有用的核心原则,也是许多表现模式背后的驱动力量.在WPF和Silverlight开发中,MVVM成为了实现关注分离最为有效的设计模式.然而,这种 ...

  10. delphi程序向另一个可执行程序发消息(使用GetForegroundWindow; 找出当前操作系统中活动的第一个窗口)

    function FindWindowThroughWindowText(WindowText: string): THandle;var  hCurrentWindow: THandle;  cnt ...