递归是一种自己调用自己的编程技术。
1、三角数字
1、3、6、10、15、21……第n项=n-1项的值+n
package Structure;import java.io.IOException;import java.io.InputStreamReader;import java.io.BufferedReader;public class triangleD { static int theNumber; public static void main(String[] args)throws IOException{ System.out.print("Enter a number: "); theNumber = getInt(); int theAnswer = triangle(theNumber); System.out.println("Trangle=" + theAnswer); } //三角数//递归实现 public static int triangle(int n){ if(n==0) return 1; else return(n+triangle(n-1));//将+变*为求阶乘 } public static String getString() throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; }/*从键盘读入*/ public static int getInt()throws IOException{ String s = getString(); //Parses the string argument as a signed decimal integer. return Integer.parseInt(s); }}
2、递归算法:
1)、调用自身
2)、解决比本步更小的问题
3)、存在足够简单的不需要调用自身就能得到的值,如n=1时三角数的第一项值为1。
package Structure;/*全排列*/import java.io.IOException;import java.io.InputStreamReader;import java.io.BufferedReader;public class TwoQuanPaiLie { static int size; static int count; static char[] arrChar = new char[100]; public static void main(String[] args) throws IOException{ System.out.print("Enter a word :"); String input = getString(); size = input.length(); count = 0; for(int j=0;j<99) System.out.print(" "); if(count<9) System.out.print(" "); System.out.print(++count+" "); for(int j=0;j
3、递归实现二分查找
package Structure;class OrdArray{ private long[] a; private int nElems; public OrdArray(int max){ a = new long[max]; nElems = 0; } public int size(){ return nElems; } /*public int find(long searchKey){ int lowerBound = 0; int upperBound = nElems - 1; int curIn; while(true){ curIn = (lowerBound + upperBound) / 2; if(a[curIn]==searchKey) return curIn; else if(lowerBound>upperBound) return nElems; else{ if(a[curIn]upperBound) return nElems; else{ if(a[curIn] value) break; for(int k=nElems;k>j;k--) a[k] = a[k-1]; a[j] = value; nElems++; }//end insert public boolean delete(long value ){ int j = find(value, 0, nElems - 1); if(j==nElems) return false; else{ for(int k=j;k
4、汉诺塔问题
将第一个柱子上的盘子按现在同样的顺序移到最后一个柱子上,可以使用中间的柱子。
package Structure;public class SixTowers { static int nDisks = 3; public static void doTowers(int topN,char from,char inter,char to){ if(topN==1) System.out.println("Disk 1 from "+from+" to "+to); else{ doTowers(topN-1,from,to,inter); System.out.println("Disk "+topN+" from "+from+" to "+to); doTowers(topN-1,inter,from,to); } } public static void main(String[] args){ doTowers(nDisks,'A','B','C'); }} /*Outputs:Disk 1 from A to CDisk 2 from A to BDisk 1 from C to BDisk 3 from A to CDisk 1 from B to ADisk 2 from B to CDisk 1 from A to C*/
5、归并排序
归并两个已经有序的数组。
package Structure;/*归并排序,注意待归并的两个数组都是有序的*/public class SixGuiBing { public static void guiBing(int[] arrA,int sizeA,int[] arrB,int sizeB,int arrC[]){ int aDex = 0,bDex = 0,cDex = 0; while(aDex < sizeA && bDex < sizeB){ if(arrA[aDex] < arrB[bDex]) arrC[cDex++] = arrA[aDex++]; else arrC[cDex++] = arrB[bDex++]; }/*复制小的到arrC*/ while(aDex < sizeA) arrC[cDex++] = arrA[aDex++];//arrB排空了,arrA未空 while(bDex < sizeB) arrC[cDex++] = arrB[bDex++];//arrA排空了,arrB未空 } public static void display(int[] theArr,int size){ for(int j=0;j