The recursive algorithm is beautiful (factorial, Fibonacci, print elements, insertion sort, Tower of Hanoi)

// recursive thinking
// (1) find duplicate find or identify the division method recurrence formula, is the parent problem into sub-problems
// amount (2) change change should look for as a parameter
// (3) to find the boundary to prevent death (loop must be changed recursively)
@ + can be decomposed into a small amount directly subproblems
// may be sub-divided into a plurality of small-scale problem
public class Whatdigui {
// F1 factorial method (truncated thinking) f (n-) = F n-* (. 1-n-)
public static int F1 (n-int) {
IF (n-==. 1)
return. 1;
the else
return * n-F1 (n--. 1);
}
// print F2 i to j The method (truncated thinking) P (I, J) = P (I) -> P (I +. 1, J)
public static void F2 (int I, int J) {
IF (I> J) {
return;
}
the System .out.println (I);
F2 (. 1 + I, J);
}
// F3 all of the elements in the array arr summing method (truncated thinking)
public static int F3 (int arr [], int the begin) {
if (begin == arr.length - 1) {
ARR return [the begin];
}
return ARR [the begin] + F3 (ARR, the begin + 1'd);
}
// string Reverse inversion method (tailing thinking)
public static Reverse String (String S, int End) {
IF ( == 0 End) {
return "" + s.charAt (0);
}
the else {
return s.charAt (End) + Reverse (S, End -. 1);
}
}
// the Fibonacci columns F4 (recursive formula) F (n-) = F (n--. 1) + F (n--2);
public static int F4 (n-int) {
IF (n-n-||. 1 == == 2) {
return. 1;
}
return F4 (. 1-n) + F4 (n-2);
}
// greatest common divisor of n and m (the recursion formulas) F (m, n) = F (n, n m%);
public static int GCD (int m, n-int) {
IF (n-== 0) {
return m;
}
the else {
return GCD (n-,% n-m);
}
}
// sort the array, 0 is equivalent to the second to last element of the array is sorted, etc.
// last element into the last part of the orderly
public static void insertSort (int arr [ ], int k) {
IF (k == 0) {
return;
}
// for the k-1 before sorting elements
insertSort (ARR, k-1);
// k is the element position to the front portion inserted
int x = arr [K];
int index = K -. 1;
the while (X <ARR [index]) {
ARR [index +. 1] = ARR [index];
index -;
}
ARR [index] = X;
}
// HANOR the step of moving the
@ 1-N will move from a to B, C as an auxiliary
// equivalent to 1 to N-1 will move from a to C, N is moved from the a B, and from 1 to N-1 C moves to B
public static void printHanoiTower (int N, String from, String to, String Help) {
/ *
the N plate from source to move to the target path printing
N initial N number of small to large plates, N being the maximum number
from 原始柱子
help 辅助的柱子
to 目标的柱子
*/
if (N == 1) {
System.out.println(“move” + N + “from” + from + “to” + to);
return ;
}
else {
printHanoiTower(N-1, from, help, to) ;
System.out.println(“move” + N + “from” + from + “to” + to);
printHanoiTower(N-1, help,to,from) ;
}
}
public static void main(String[] args) {
f2 (1,4) ;
System.out.println(reverse(“abcde”,4));
System.out.println(f4(5)) ;
System.out.println(gcd(3,6));
}
}

Published 39 original articles · won praise 17 · views 2174

Guess you like

Origin blog.csdn.net/nuist_NJUPT/article/details/104320613
Recommended