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

java递归算法

发布时间:2022-02-06 22:11:38

java的递归方法

横线填:d2b(n/2);

❷ java递归实现折中算法

public class TestSort<O extends Comparable<O>> {
private O[] data;
public TestSort(O[] data)
{
this.data = data;
}
/**
* 递归折中算法
*/
public int sortByReturn(int low, int high, O value)
{
int mid = (low+high)/2;
if(value.compareTo(data[mid])<0)
{
high = mid -1;
return sortByReturn(low,high,value);
}else if(value.compareTo(data[mid])>0)
{
low = mid + 1;
return sortByReturn(low,high,value);
}else
{
return mid;
}
}
/**
* 循环折中算法
* @param args
*/
public int sortByWhile(O value)
{
if(value==null)
{
return 0;
}
int low = 0;
int high = data.length-1;
int mid ;
while(low<=high)
{
mid = (high+low)/2;
if(value.compareTo(data[mid])<0)
{
high = mid-1;
}else if(value.compareTo(data[mid])>0)
{
low = mid+1;
}else if(value.compareTo(data[mid])==0)
{
return mid;
}
}
return -1;
}
public static void main(String[] args)
{
Integer data[] = {1,2,5,7,9,33,43,45,66,78,93};
TestSort<Integer> ts = new TestSort<Integer>(data);
System.out.println("sort by while:"+ts.sortByWhile(5));
System.out.println("sort2 by while:"+ts.sortByReturn(0,data.length-1,33));
}
}

❸ 用java递归算法,求1+2+4+8+~~~~~的和

即f(x)=f(x-1)*2;f(1)=1;......
public class Test4 {
public static void main(String[]args){
System.out.println(num(10));
}

public static int f(int x){
if(x == 1)return 1;
return f(x - 1)*2;
}

public static int num(int x){
if(x == 1)return f(1);
return num(x - 1)+f(x);
}
}

❹ 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递归算法的例子。

阶乘:

要求:给定一个数值,计算出它的阶乘值,例如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中的递归方法,求讲一下。

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

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

int fact(int i){

if(i<=1)return 1;

return fact(i-1)*i;

}

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

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

❼ 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递归算法的例子

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

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

阅读全文

与java递归算法相关的资料

热点内容
linuxapachephp56 浏览:390
安卓手机如何打开eng文件 浏览:22
看拉丁电视都用什么app好 浏览:778
什么是哲学pdf 浏览:507
hdfs的三个下载命令 浏览:522
java常用的排序算法 浏览:357
51单片机连接adc 浏览:859
python命名变量报错 浏览:120
安卓手机如何换windows系统 浏览:612
python中的类是什么 浏览:630
我的英雄学院用哪个app可以看 浏览:35
excel插入选项卡对象命令 浏览:693
python字符全排列 浏览:505
824页大瓜文件pdf 浏览:222
朔州ios源码 浏览:251
算法逻辑电路 浏览:941
青少年喝酒解压辩论赛 浏览:175
android如何新建activity 浏览:741
ntp支持的认证算法 浏览:716
想做快手主播需要什么app 浏览:926