导航:首页 > 源码编译 > 递归算法java

递归算法java

发布时间:2022-02-16 13:44:21

A. java中的递归方法,求讲一下。

自己调用自己或几个方法相互调用。

最经典的是求正整数阶的算法

int fact(int i){

if(i<=1)return 1;

return fact(i-1)*i;

}

多数递归方法可以转换成非递归方法。

一般同功能的非递归方法,执行效率要优于递归方法。但合理的使用递归方法,可以使代码结构更清晰,更有可读性,从而更方便维护。

B. java递归算法写1+到100的和

我只把这个函数写出来: public int Cumulative(int n) { if(n==1) return 1; else return n+f(n-1);}

C. JAVA递归算法,1^2+2^2+3^2+....+n^2

publicclassDigui{//递归算法:publicstaticdoubledigui(intn){doublesum=0.0;if(n==1){sum=1;}else{if(n%2==0){sum=1.0/n+digui(n-1);}else{sum=-1.0/n+digui(n-1);}}returnsum;}//非递归算法:publicstaticdoublefeidigui(intn){doublecount=0.0;StringBuffersb=newStringBuffer();for(doublei=1;i<=n;i++){if(i==1){count=1;sb.append("n!="+n+"!=1");}else{if(i%2==0){count=count+1/i;sb.append("+1/"+(int)i);}else{count=count-1/i;sb.append("-1/"+(int)i);}}}System.err.println(sb.toString());returncount;}publicstaticvoidmain(String[]args){intn=10;doubledigui=feidigui(n);doublefeidigui=digui(n);System.out.println("递归算法:"+n+"!="+digui);System.out.println("非递归算法:"+n+"!="+feidigui);}}运行结果如下:n!=10!=1+1/2-1/3+1/4-1/5+1/6-1/7+1/8-1/9+1/10递归算法:10!=1.3543650793650797非递归算法:10!=1.3543650793650797

D. java中递归算法是什么怎么算的

Java递归算法是基于Java语言实现的递归算法。递归算法是一种直接或者间接调用自身函数或者方法的算法。递归算法实质是把问题分解成规模缩小的同类问题的子问题,然后递归调用方法表示问题的解。递归往往能给我们带来非常简洁非常直观的代码形式,从而使我们的编码大大简化,然而递归的思维确实跟我们的常规思维相逆的,通常都是从上而下的思维问题,而递归趋势从下往上的进行思维。

二、递归算法解决问题的特点:

【1】递归就是方法里调用自身。

【2】在使用递归策略时,必须有一个明确的递归结束条件,称为递归出口。

【3】递归算法代码显得很简洁,但递归算法解题的运行效率较低。所以不提倡用递归设计程序。

【4】在递归调用的过程中系统为每一层的返回点、局部量等开辟了栈来存储。递归次数过多容易造成栈溢出等,所以一般不提倡用递归算法设计程序。

【5】在做递归算法的时候,一定把握出口,也就是做递归算法必须要有一个明确的递归结束条件。这一点是非常重要的。其实这个出口就是一个条件,当满足了这个条件的时候我们就不再递归了。

三、代码示例:

代码执行流程图如下:

此程序中n=5就是程序的出口。

E. java递归算法

1.汉诺塔问题
import javax.swing.JOptionPane;
public class Hanoi {
private static final String DISK_B = "diskB";
private static final String DISK_C = "diskC";
private static final String DISK_A = "diskA";
static String from=DISK_A;
static String to=DISK_C;
static String mid=DISK_B;
public static void main(String[] args) {
String input=JOptionPane.showInputDialog("please input the number of the disks you want me move.");
int num=Integer.parseInt(input);
move(num,from,mid,to);
}
private static void move(int num, String from2, String mid2, String to2) {
if(num==1){
System.out.println("move disk 1 from "+from2+" to "+to2);
}
else {
move(num-1,from2,to2,mid2);
System.out.println("move disk "+num+" from "+from2+" to "+to2);
move(num-1,mid2,from2,to2);
}
}
}
2. 这是一个排列的例子,它所做的工作是将输入的一个字符串中的所有元素进行排序并输出,例如:你给出的参数是"abc" 则程序会输出:
abc
acb
bac
bca
cab
cba
(1)算法的出口在于:low=high也就是现在给出的排列元素只有一个时。
(2)算法的逼近过程:先确定排列的第一位元素,也就是循环中i所代表的元素,
然后low+1开始减少排列元素,如此下去,直到low=high
public static void permute(String str) {
char[] strArray = str.toCharArray();
permute(strArray, 0, strArray.length - 1);
}
public static void permute(char[] list, int low, int high) {
int i;
if (low == high) {
String cout = "";
for (i = 0; i <= high; i++)
cout += list[i];
System.out.println(cout);
} else {
for (i = low; i <= high; i++) {
char temp = list[low];
list[low] = list[i];
list[i] = temp;
permute(list, low + 1, high);
temp = list[low];
list[low] = list[i];
list[i] = temp;
}
}
}
3。这是一个组合的例子,与上述的例子相似,只是它所做的工作是,输出所给字符串中制定数目的元素的组合种类
(1)程序出口在于n=1,此时只要输出目标数组的所有元素即可
(2)逼近过程,当n>1的时候,我们先取第一个元素放入目标数组中,然后n-1,如此下去,最后出来。
import javax.swing.JOptionPane;
public class Combination {
/**
* @param args
*/
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("please input your String: ");
String numString = JOptionPane.showInputDialog("please input the number of your Combination: ");
int num = Integer.parseInt(numString);
Combine(input, num);
}
private static void Combine(String input, int num) {
char[] a = input.toCharArray();
String b = "";
Combine(a, num, b, 0, a.length);
}
private static void Combine(char[] a, int num, String b, int low, int high) {
if (num == 0) {
System.out.println(b);
} else {
for (int i = low; i < a.length; i++) {
b += a[i];
Combine(a, num - 1, b, i+1, a.length);
b=b.substring(0, b.length()-1);
}
}
}
}

F. 用java递归方法实现

publicintfun(intn){
if(n==0||n==1)return1;
returnn*fun(n-1);
}

G. java用递归算法编写求1~n的累加和

importjava.util.Scanner;
publicclassZhi2{
publicstaticvoidmain(String[]args){
intnumber=1;
intsum=0;
intn;
Scannerscanner=newScanner(System.in);
n=scanner.nextInt();
for(;number<=n;number++){
sum+=number;
}
System.out.println(+sum);
}
}

H. java递归算法的例子

十进制整数转二进制字符串的递归写法:

public String dtob(int n) {
if (n == 0 || n == 1) {
return Integer.toString(n);
} else {
return dtob(n / 2) + Integer.toString(n % 2);
}
}

I. java递归算法的例子。

阶乘:

要求:给定一个数值,计算出它的阶乘值,例如5的阶乘为5*4*3*2*1

实现:

[html] view plain

<span style="font-size:12px;"> // 利用递归实现一个数的阶乘值 private static BigDecimal getNum(BigDecimal inNum) { if (inNum.compareTo(BigDecimal.ONE) == 0) { return inNum; } return inNum.multiply(getNum(inNum.subtract(BigDecimal.ONE))); }</span>

(2)Fibonacci数列:1,1,2,3,5,8,13……

要求:找出数列中指定index位置的数值

实现:

[html] view plain

<span style="font-size:12px;"> // 利用递归实现了Fibonacci数列 private static int fab(int index) { if (index == 1 || index == 2) { return 1; } else { return fab(index - 1) + fab(index - 2); } }</span>

(3)汉诺塔

要求:汉诺塔挪动

实现:

[html] view plain

<span style="font-size:12px;"> <span style="white-space:pre;"> </span>private static final String DISK_B = "diskB"; <span style="white-space:pre;"> </span>private static final String DISK_C = "diskC"; <span style="white-space:pre;"> </span>private static final String DISK_A = "diskA"; <span style="white-space:pre;"> </span>static String from=DISK_A; <span style="white-space:pre;"> </span> static String to=DISK_C; <span style="white-space:pre;"> </span> static String mid=DISK_B; <span style="white-space:pre;"> </span> public static void main(String[] args) { <span style="white-space:pre;"> </span> String input=JOptionPane.showInputDialog("please input the number of the disks you want me move."); <span style="white-space:pre;"> </span> int num=Integer.parseInt(input); <span style="white-space:pre;"> </span> move(num,from,mid,to); <span style="white-space:pre;"> </span> }</span>

[html] view plain

<span style="font-size:12px;"> // 利用递归实现汉诺塔 private static void move(int num, String from2, String mid2, String to2) { if (num == 1) { System.out.println("move disk 1 from " + from2 + " to " + to2); } else { move(num - 1, from2, to2, mid2); System.out.println("move disk " + num + " from " + from2 + " to " + to2); move(num - 1, mid2, from2, to2); } }</span>

(4)排列组合

要求:将输入的一个字符串中的所有元素进行排序并输出,例如:你给出的参数是"abc",

则程序会输出

abc

acb

bac

bca

cab

cba

实现:

[html] view plain

<span style="font-size:12px;"><span style="white-space:pre;"> </span>public static void permute(String str) { <span style="white-space:pre;"> </span> char[] strArray = str.toCharArray(); <span style="white-space:pre;"> </span> permute(strArray, 0, strArray.length - 1); <span style="white-space:pre;"> </span>}</span>

[html] view plain

<span style="font-size:12px;"> // 利用递归实现,将输入的一个字符串中的所有元素进行排序并输出 public static void permute(char[] list, int low, int high) { int i; if (low == high) { String cout = ""; for (i = 0; i <= high; i++) { cout += list[i]; } System.out.println(cout); } else { for (i = low; i <= high; i++) { char temp = list[low]; list[low] = list[i]; list[i] = temp; permute(list, low + 1, high); temp = list[low];

J. 用递归算法求1~100的和,用java写。

publicclassTest{/*创建类*/

publicstaticvoidmain(String[]args){
System.out.println(dg(100));
}

staticintdg(inti){/*定义变量*/
intsum;
if(i==1)/*假设条件*/
return1;
else
sum=i+dg(i-1);/*1~100的和的表达式*/
returnsum;/*返回结果*/
}
}

递归是计算机科学的一个重要概念,递归的方法是程序设计中有效的方法,采用递归编写。

递归能使程序变得简洁和清晰。

每次调用在规模上都有所缩小(通常是减半);

相邻两次重复之间有紧密的联系,前一次要为后一次做准备(通常前一次的输出就作为后一次的输入);

在问题的规模极小时必须用直接给出解答而不再进行递归调用,因而每次递归调用都是有条件的(以规模未达到直接解答的大小为条件),无条件递归调用将会成为死循环而不能正常结束。

阅读全文

与递归算法java相关的资料

热点内容
混凝土结构中册pdf 浏览:927
永劫无间解压不了怎么回事 浏览:806
php如何开启curl 浏览:670
红黄文件夹 浏览:122
违背皇帝的命令是死罪吗 浏览:66
phpcurl处理错误 浏览:459
linuxftp防火墙端口设置 浏览:788
java面板图片 浏览:482
泰拉瑞亚14安卓版怎么操作 浏览:718
安卓手机相册加密软件 浏览:51
免费云服务器能永久使用吗 浏览:703
吉利配件大全吉利压缩机 浏览:817
人类怎么能听出小猫的叫声app 浏览:904
大众安卓大屏如何显示原车信息 浏览:550
纸质电话数据加密法 浏览:178
linux弹出光盘命令 浏览:272
java加密jar包防止反编译 浏览:402
redhatlinux安装mysql 浏览:695
怎么把word和ppt放在一个文件夹 浏览:142
pdf优化器 浏览:134