Java经典递归算法
2020-12-13 04:29
标签:斐波那契数 port span length 相加 multi 经典 依次 out 九九 Java经典递归算法 标签:斐波那契数 port span length 相加 multi 经典 依次 out 原文地址:https://www.cnblogs.com/gaobing1252/p/11110935.html斐波那契数列
package com.luna.base;
public class BirthRabbit {
public static void main(String[] args) {
int i = 1;
for (i = 1; i ) {
System.out.println("兔子第" + i + "个月的总数为:" + f(i));
}
}
public static int f(int x) {
if (x == 1 || x == 2) {
return 1;
} else {
return f(x - 1) + f(x - 2);
}
}
}
从1到100相加
package com.luna.base;
public class Plus {
public int sum(int i) {
if (i == 1) {
return 1;
}
return i + sum(i - 1);
}
public static void main(String[] args) {
Plus plus = new Plus();
System.out.println("计算结果:" + plus.sum(100) + "!");
}
}
100的阶乘
package com.luna.base;
import java.math.BigInteger;
public class LoopMutiply {
public BigInteger sum(int i) {
if (i == 1) {
return BigInteger.ONE;
}
return BigInteger.valueOf(i).multiply(sum(i - 1));
}
public static void main(String[] args) {
LoopMutiply test = new LoopMutiply();
try {
System.out.println("计算结果:" + test.sum(50) + "!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
有序数组a、b合并成一个新的有序数组
package com.luna.base;
public class ArraySort {
public static void main(String[] args) {
int[] a = { 1, 3, 4 };
int[] b = { 2, 3, 5, 6 };
int[] c = mergeArray(a, b);
for (int n : c) {
System.out.print(n + " ");
}
}
// 合并数组
public static int[] mergeArray(int[] a, int[] b) {
int result[] = new int[a.length + b.length];
if (checkSort(a) && checkSort(b)) {
// 说明ab数组都是有序的数组
// 定义两个游标
int i = 0, j = 0, k = 0;
while (i b.length) {
if (a[i] b[j]) {
result[k++] = a[i++];
} else {
result[k++] = b[j++];
}
}
while (i a.length) {
// 说明a数组还有剩余
result[k++] = a[i++];
}
while (j b.length) {
result[k++] = b[j++];
}
}
return result;
}
// 检查一个数组是否是有序1 2 3
public static boolean checkSort(int[] a) {
boolean flag = false;// 默认不是有序的
for (int i = 0; i ) {
if (a[i] > a[i + 1]) {
// 说明不是有序的
flag = false;
break;
} else {
flag = true;
}
}
return flag;
}
}
归并排序算法实现
package com.luna.base;
public class MergingSort {
public static void sort(int[] data, int left, int right) {
if (left right) {
// 首先找出中间的索引
int center = (left + right) / 2;
// 对中间索引左边的数组进行递归
sort(data, left, center);
// 对中间索引右边的数组进行递归
sort(data, center + 1, right);
// 合并
merge(data, left, center, right);
}
}
public static void merge(int[] data, int left, int center, int right) {
int[] tmpArr = new int[data.length];
int mid = center + 1;
// third记录中间数组的索引
int third = left;
int tmp = left;
while (left right) {
// 将两个数组中取出最小的数放入中间数组
if (data[left] data[mid]) {
tmpArr[third++] = data[left++];
} else {
tmpArr[third++] = data[mid++];
}
}
// 剩余部分依次放入中间数组
while (mid right) {
tmpArr[third++] = data[mid++];
}
while (left center) {
tmpArr[third++] = data[left++];
}
while(tmp right){
data[tmp] = tmpArr[tmp++];
}
}
public static void main(String[] args) {
int[] a = { 3, 2, 5, 4 };
sort(a, 0, a.length - 1);
for (int i = 0; i ) {
System.out.print(a[i] + " ");
}
}
}
public static void main(String[] args) {
for (int i = 1 ; i ){
for (int j = 1 ; j ){
System.out.print(i + "*" + j + "=" + i * j + " ");
}
System.out.println();
}
}
上一篇:十四、线程设计