递归是一种自己调用自己的编程技术。
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<size;j++){ arrChar[j] = input.charAt(j); } doAnagram(size); } public static void doAnagram(int newSize){ if(newSize==1) return; for(int j=0;j<newSize;j++){ doAnagram(newSize-1); if(newSize==2) displayWord(); rotate(newSize); } public static void rotate(int newSize){ int j; int position = size-newSize; char temp = arrChar[position]; for(j=position+1;j<size;j++) arrChar[j-1] = arrChar[j]; arrChar[j-1] = temp; } public static void displayWord(){ if(count<99) System.out.print(" "); if(count<9) System.out.print(" "); System.out.print(++count+" "); for(int j=0;j<size;j++) System.out.print(arrChar[j]); System.out.print(" "); System.out.flush(); if(count%6==0) System.out.println(""); } public static String getString()throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } }}/*Outputs: Enter a word :cat 1 cat 2 cta 3 atc 4 act 5 tca 6 tac */
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]<searchKey) lowerBound = curIn + 1; else upperBound = curIn - 1; } } }*///end find //递归实现二分查找 public int find(long searchKey,int lowerBound,int upperBound){ int curIn; curIn = (lowerBound + upperBound) / 2; if(a[curIn]==searchKey) return curIn; else if(lowerBound > upperBound) return nElems; else{ if(a[curIn]<searchKey) return find(searchKey,curIn+1,upperBound); else return find(searchKey,lowerBound,curIn-1); } }//end find public void insert(long value){ int j; for(j=0;j<nElems;j++) if(a[j]>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<nElems;k++) a[k] = a[k+1]; nElems--; return true; } }//end delete public void display(){ for(int j=0;j<nElems;j++) System.out.print(a[j]+" "); System.out.println(""); } } public class TwoDiGuiOrderedArray { public static void main(String[] args){ int maxSize = 100; OrdArray arr; arr = new OrdArray(maxSize); arr.insert(12); arr.insert(103); arr.insert(131); arr.insert(113); arr.insert(123); arr.insert(133); int searchKey = 55; if(arr.find(searchKey,0,(arr.size()-1))!=arr.size()) System.out.println("find " + searchKey); else System.out.println("Can't find " + searchKey); arr.display(); arr.delete(13); arr.delete(103); arr.display(); } }
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 C Disk 2 from A to B Disk 1 from C to B Disk 3 from A to C Disk 1 from B to A Disk 2 from B to C Disk 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<size;j++) System.out.print(theArr[j]+" "); System.out.println(""); } public static void main(String[] args){ int[] arrA = {10,12,20,21,30}; int[] arrB = {0,2,3,11,23}; int[] arrC = new int[10]; guiBing(arrA,5,arrB,5,arrC); display(arrC,10); } }/*Output:0 2 3 10 11 12 20 21 23 30 */