阶乘
public class Factorial {
public static void main(String
[] args
) {
System
.out
.print("请输入你想要计算的数字:");
Scanner sc
= new Scanner(System
.in
);
int num
= sc
.nextInt();
long result
= factorial(num
);
System
.out
.println(num
+ "的阶乘是:" + result
);
}
public static long factorial(int num
) {
if(num
== 0){
return 1;
} else {
return num
* factorial(num
- 1);
}
}
}
斐波那契数列
public class ComputeFibonacci {
public static void main(String
[] args
) {
System
.out
.print("请输入你想要计算的数字:");
Scanner sc
= new Scanner(System
.in
);
int index
= sc
.nextInt();
long result
= fibonacci(index
);
System
.out
.println("最终结果是:" + result
);
}
public static long fibonacci(long index
) {
if(index
== 0) {
return 0;
}else if (index
== 1){
return 1;
}else {
return fibonacci(index
-1) + fibonacci(index
-2);
}
}
}
判断回文串
public class RecursivePalindrome {
public static void main(String
[] args
) {
System
.out
.println("Is moon a palindrome?" + "\t" + isPalindrome("moon"));
System
.out
.println("Is noon a palindrome?" + "\t" + isPalindrome("noon"));
System
.out
.println("Is a a palindrome?"+ "\t" + isPalindrome("a"));
System
.out
.println("Is aba a palindrome?" + "\t" + isPalindrome("aba"));
System
.out
.println("Is ab a palindrome?" + "\t" + isPalindrome("ab"));
}
public static boolean isPalindrome(String s
) {
return isPalindrome(s
, 0, s
.length()-1);
}
public static boolean isPalindrome(String s
, int low
, int high
) {
if(high
<= low
) {
return true;
}else if (s
.charAt(0) == s
.charAt(s
.length() - 1)) {
return false;
}else {
return isPalindrome(s
, low
+1, high
-1);
}
}
}
递归二分查找
public class RecursiveBinarySearch {
public static void main(String
[] args
) {
int[] list1
= {1,1,2,7,9,3,4,5,6,8};
Arrays
.sort(list1
);
for (int i
: list1
) {
System
.out
.println(i
);
}
System
.out
.println(recursiveBinarySearch(list1
, 1));
}
public static int recursiveBinarySearch(int[] list
, int key
) {
int low
= 0;
int high
= list
.length
-1;
return recursiveBinarySearch(list
, key
, low
, high
);
}
private static int recursiveBinarySearch(int[] list
, int key
,int low
, int high
) {
if(low
> high
) {
return -low
-1;
}
int mid
= (low
+ high
) / 2;
if(key
< list
[mid
]) {
return recursiveBinarySearch(list
, key
, low
, mid
-1);
}else if(key
== list
[mid
]){
return mid
;
}else {
return recursiveBinarySearch(list
, key
, mid
+1, high
);
}
}
}
递归选择排序
public class RecursiveSelectionSort {
public static void main(String
[] args
) {
double[] list
= {1, 3,5,4,2,6,5,9,7,8};
sort(list
);
for (double i
: list
) {
System
.out
.print(i
+"\t");
}
}
public static void sort(double[] list
) {
sort(list
, 0, list
.length
-1);
}
private static void sort(double[] list
, int low
, int high
) {
if(low
< high
) {
int indexOfMin
= low
;
double min
= list
[low
];
for (int i
= low
+1; i
<= high
; i
++) {
if(list
[i
] < min
) {
min
= list
[i
];
indexOfMin
= i
;
}
}
list
[indexOfMin
] = list
[low
];
list
[low
] = min
;
sort(list
, low
+1, high
);
}
}
}
汉诺塔
public class TowerOfHanoi {
public static void main(String
[] args
) {
moveDisks(3, 'A','B','C');
}
public static void moveDisks(int n
, char fromTower
, char toTower
, char auxTower
) {
if (n
== 1) {
System
.out
.println("Move disk " + n
+ " from " + fromTower
+ " to " + toTower
);
}else {
moveDisks(n
-1, fromTower
, auxTower
, toTower
);
System
.out
.println("Move disk " + n
+ " from " + fromTower
+ " to " + toTower
);
moveDisks(n
-1, auxTower
, toTower
, fromTower
);
}
}
}
转载请注明原文地址: https://mac.8miu.com/read-452095.html