One, simple demo
public class Recursion {
public static void Main (string[] args) {
Test (100);
}
public static void Test (int n) {
if (n = = 0) {
Return
}
SYSTEM.OUT.PRINTLN (n);
Test2 (n-1);
}
}
Two, triangle number
public class Triangle {
public static int GetNumber (int n) {
int total= 0;
while (n > 0) {
Total = Total + N;
n--;
}
return total;
}
public static int getnumberbyrecursion (int n) {
if (n = = 1) {
return 1;
} else {
return n + getnumberbyrecursion (n-1);
}
}
}
Test:
public class Testtriangle {
public static void Main (string[] args) {
System.out.println (Triangle.getnumber (500));
System.out.println (Triangle.getnumberbyrecursion (500));
}
}
Three, Fibonacci sequence.
public class Fibonacci {
public static int GetNumber (int n) {
if (n = = 1) {
return 0;
} else if (n = = 2) {
return 1;
} else {
Return GetNumber (n-1) + getnumber (n-2);
}
}
}
Test:
public class Testfibonacci {
public static void Main (string[] args) {
System.out.println (Fibonacci.getnumber (5));
}
}
This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1696501
Lapping data structures and algorithms-Application of 06 recursion