1 /*2 * recursion: The phenomenon of invoking the method itself in a method definition 3 * 4 * method of nested invocation, which is not recursive. 5 * Math.max (Math.max (A, B), c);6 * 7 * public void Show (int n) {8 * if (n <= 0) {9 * SYSTEM.EXIT (0);Ten * } One * SYSTEM.OUT.PRINTLN (n); A * Show (--N); - * } - * the * Precautions : - * A: recursion must have an exit, otherwise it is dead recursion - * B: The number of recursion can not be too many, otherwise memory overflow - * C: Construction method cannot be used recursively + * - * Public Diguidemo () { + * Diguidemo (); A * } at * - * Examples: - * A: There used to be a mountain, a temple in the mountains, an old monk and a young monk in the temple, the old monk telling stories to the Little Monk, the story is: - * There used to be a mountain, a temple in the mountains, an old monk and a little monk in the temple, the old monk telling stories to the Little Monk, the story is: - * There used to be a mountain, a temple in the mountains, an old monk and a little monk in the temple, the old monk telling stories to the Little Monk, the story is: - * There used to be a mountain, a temple in the mountains, an old monk and a little monk in the temple, the old monk telling stories to the Little Monk, the story is: in * ... - * Exit: The temple is hung, or a landslide. to * B: Learning Programming--high salary employment--earning money--marrying a daughter -in-law-----------earning tuition + * Learn programming--high salary employment--earning money--marrying a daughter -in-law----------- - * Learn programming--high salary employment--earning money--marrying a daughter -in-law----------- the * Learn programming--high salary employment--earning money--marrying a daughter -in-law----------- * * ... $ * Export: Can't marry a daughter-in-law or give birth to a dollPanax Notoginseng */ - the + PackageZl_digui; A /* the There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits every month, the small rabbit to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask the 20th month of the number of rabbits? + Analysis: - Rabbit logarithm $ First Month: 1 $ Second Month: 1 - Third Month: 2 - Fourth Month: 3 the Fifth Month: 5 - Sixth Month: 8Wuyi 1,1,2,3,5,8 ... the Rules: - A: Starting with the third item, each item is the sum of the first two Wu B: The first and second items are known - Method: About A: Array $ B: Change of variable realization - The first month of a=1,b=1; - The second month of a=1,b=2; - a=2,b=3 of the third month; A fourth month of a=3,b=5; + ... the each month's a, is the last month's B, each month's B, is last month's a+b - C: Recursion $ A: return type int the B: Parameter list int n (month) the c: Export: n = 1 | | n ==2 when it is the first month or the second month the the */ - Public classDiGuiDemo1 { in the Public Static voidMain (string[] args) { the About //First method: Array the int[] ary =New int[20]; theAry[0] = 1; theARY[1] = 1; + - for(intx = 2; x < ary.length; X + +){ theARY[X] = ary[x-1] + ary [x-2];Bayi } theSystem.out.println (ary[19]); the - //the second method: the change of variable realization - intA = 1; the intb = 1; the for(inty = 3; Y <= 20; Y + +){ the inttemp =A; theA =b; -b = temp +A; the } the System.out.println (b); the 94 //Call Recursion theSystem.out.println (Digui (20)); the } the //Recursive98 Public Static intDigui (intN) { About if(n = = 1 | | n = = 2){ - return1;101}Else{102 returnDigui (n-1) + Digui (n-2);103 }104 } the}
Thinking and illustration of recursive problem solving
Code implementation of recursive factorial and memory plot
Overview and case studies of Java 20-1 recursion