java中的递归算法
❶ 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
❷ java递归算法,怎么理解
n! = (n-1)*n!
简单理解,就是目前的所有任务,等于前面所有的任务+现在的任务。
比如你求 1。。。100的加法总和
实际上是 1... 99 的加法总和 + 100 就是了。
这就是递归的来源。
你只需要计算你前一步的任务,然后加上自己,就OK了。
前一步,在再次调用前前一步......
❸ 用java递归方法实现
publicintfun(intn){
if(n==0||n==1)return1;
returnn*fun(n-1);
}
❹ 在JAVA中什么是递归有什么用
Java方法递归是指在一个方法的内部调用自身的过程,以此类推就是java方法递归的理解思想,具体来讲就是把规模大的问题转化为规模小的相似的子问题来解决。在函数实现时,因为解决大问题的方法和解决小问题的方法往往是同一个方法,所以就产生了函数调用它自身的情况。另外这个解决问题的函数必须有明显的结束条件,这样就不会产生无限递归的情况了。因此,java方法递归的两个条件就是,一通过递归调用来缩小问题规模,且新问题与原问题有着相同的形式;二存在一种简单情境,可以使递归在简单情境下退出。
❺ java递归算法的例子
简单理解一下:
递归就是调用上一步的结果,来产生下一步的结果,一般上一步的结果是由函数得到,所以下一步直接调用函数,参数不一样而已:
阶乘算法:
public int JieCheng(int n){
return n*JieCheng(n-1);
}
当然还必要一些条件判断吗,就是终止递归调用的,jiecheng为n=1 时 就return 1;
❻ 用java递归算法求一个数字的阶乘
用递归算法求一个数字的阶乘的程序如下:
public class JieCheng {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入一个整数:");
int n = in.nextInt();
System.out.println(n+"!="+f(n));
}
static long f(int n){
if (n==1) return 1;
else return n*f(n-1);
}
}
运行结果:
请输入一个整数:6
6!=720
❼ 在java中,用递归方法计算n的阶乘。
用Java求键盘输入的数的阶乘n。(递归算法)packagejiecheng; importjava.util.*; //导入java.util包中的所有类classrep{ publiclongrep(intn){ longi=0; if(n==0||n==1) i=1;
elsi=n*rep(n-1) returni; } } publicclassJie{ publicstaticvoidmain(String[]args){ intn; //此处定义要输入的数Scanners= newScanner(System.in); //以下三行用于n的值得输入System.out.print( "请输入一个整数:"); n=s.nextInt(); repf= newrep(); System.out.println(n+"!="+f.rep(n)); } }
❽ 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];
❾ 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);
}
}
}
}
❿ JAVA如何理解递归
1、递归做为一种算法在程序设计语言中广泛使用,是指函数/过程/子程序在运行过程中直接或间接调用自身而产生的重入现象。
2、递归算法一般用于解决三类问题:
1)数据的定义是按递归定义的。(Fibonacci(斐波那契)的函数)
2)问题解法按递归算法实现。(回溯)
3)数据的结构形式是按递归定义的。(树的遍历,图的搜索)