import java.util.HashMap;
public class JavaTest {
public static void main(String[] args) {
//1.斐波那契数列
/*
for (int i = 1; i <= 20; i++) {
System.out.println("兔子第" + i + "个月的总数为:" + f(i));
}
*/
//2、1+,,,,+100 求和
//System.out.println("1+,,,,+100 求和:"+fum(100));
//System.out.println(fum1(10));
/*
int[] a = {1, 3, 4};
int[] b = {2, 3, 5, 6};
int[] c = merge(a, b);
for (int n : c) {
System.out.print(n + " ");
}
*/
ListNode a = new ListNode(1);
ListNode b = new ListNode(2);
ListNode c = new ListNode(3);
ListNode d = new ListNode(4);
a.next = b;
b.next = c;
c.next = d;
//ListNode result = reverse(a);
//System.out.print(result);
//reverse2(a);
//冒泡排序算法
//int[] numbers = new int[]{4, 5, 6, 3, 2, 1};
// 第1轮:4,5,3,2,1,6
// 第2轮:4,3,2,1,5,6
// 第3轮:3,2,1,4,5,6
// 第4轮:2,1,3,4,5,6
// 第5轮:1,2,3,4,5,6
//System.out.println(bubblingSort(numbers));
//System.out.println(insertSort(numbers));
//System.out.println(test11());
//reservseArray(new int[]{1, 2, 3, 4, 5, 6});
//showDaffodilNumber();
//freeFalling(10);
}
//1.斐波那契数列
public static int f(int a) {
if (a == 1 || a == 2) {
return 1;
} else {
return f(a - 1) + f(a - 2);
}
}
//2、1+,,,,+100 求和
public static int fum(int a) {
if (a == 1) {
return 1;
}
return f(a - 1) + a;
}
//3、100的阶乘
public static int fum1(int a) {
if (a == 1) {
return 1;
}
return f(a - 1) * a;
}
//4、有序数组a、b合并成一个新的有序数组
public static int[] merge(int[] a, int[] b) {
int[] result = new int[a.length + b.length];
LinkNode tempA = new LinkNode();
LinkNode tempB = new LinkNode();
for (int i = 0; i < a.length; i++) {
tempA.a = a[i];
if (i < a.length - 1) {
LinkNode temp = new LinkNode();
temp.a = a[i + 1];
tempA.node = temp;
}
}
for (int i = 0; i < b.length; i++) {
tempB.a = b[i];
if (i < b.length - 1) {
tempB.node = new LinkNode();
}
}
while (tempA.node != null) {
int i = 0;
if (tempA.a < tempB.a) {
result[i] = tempA.a;
} else {
result[i] = tempB.a;
}
tempA = tempA.node;
}
return result;
}
/**
* 5、数组反转
*/
public static void reservseArray(int[] a) {
//采用数据对称依次相互交换值
int start = 0;//低位下标
int end = a.length - 1;//高位下标
for (; start < end; start++) {
int temp = a[start];
a[start] = a[end];
a[end] = temp;
end--;
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "\t");
}
}
/**
* 6、查找质数(素数,只有1和本身两个因数)
*/
public static void isPrimeNumber(int a) {
int result = 0;
for (int i = 1; i <= a; i++) {
if (a % i == 0) {
result++;
}
}
if (result == 2) {
System.out.println(a + ":是质数");
} else {
System.out.println(a + ":不是质数");
}
}
/**
* 7.打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
*/
public static void showDaffodilNumber() {
//方法一:
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 10; b++) {
for (int c = 0; c < 10; c++) {
int result = a * 100 + b * 10 + c;
int temp = a * a * a + b * b * b + c * c * c;
if (result == temp && result > 99) {
System.out.println(temp + " = " + a + "^3 +" + b + "^3 +" + c + "^3 ");
}
}
}
}
//方法二: 结果倒推法
for (int i = 100; i < 1000; i++) {
int a = i % 10;
int b = i / 10 % 10;
int c = i / 100;
if (a * a * a + b * b * b + c * c * c == i) System.out.println(i);
}
}
/**
* 8.
* 三个数 找最大值
*
* @param a
* @param b
* @param c
* @return
*/
public static int threeMax(int a, int b, int c) {
int max = a > b ? a : b;
max = max > c ? max : c;
return max;
}
/**
* 9.
* 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
*/
public static double freeFalling(int fallingNumber){
double s = 100;
double h = 100;
for(int a =1;a<fallingNumber;a++){
s = s+ h/2;
h = h/2;
}
System.out.println("第:"+fallingNumber + "次落下的高度时,共经过:"+s+"米");
return s;
}
public static class LinkNode {
int a;
LinkNode node;
/* public LinkNode(int a) {
this.a = a;
}*/
}
/**
* 单链
*/
public static class ListNode {
public int a;
public ListNode next;
ListNode(int a) {
this.a = a;
}
}
/**
* 链表反转 :就地反转
*
* @return
*/
public static ListNode reverse(ListNode node) {
if (node == null) {
return null;
}
ListNode dummy = new ListNode(-1);//虚拟零时用
dummy.next = node;
ListNode previous = dummy.next;//原数据
ListNode current = previous.next;//反转节点
while (current != null) {
ListNode a = current.next;
previous.next = current.next;
ListNode b = previous;
ListNode temp = dummy;
ListNode c = dummy.next;
current.next = dummy.next;
ListNode d = current;
dummy.next = current;
ListNode e = dummy;
current = previous.next;
ListNode f = current;
}
return dummy.next;
}
/**
* 新建链表,头节点插入法
*
* @return
*/
public static ListNode reverse2(ListNode node) {
if (node == null) {
return null;
}
ListNode dummy = new ListNode(-1);//虚拟零时用
ListNode current = node;//反转节点
while (current != null) {
ListNode pNex = current.next;
current.next = dummy.next;
dummy.next = current;
current = pNex;
}
return dummy.next;
}
/**
* 冒泡排序 原理:每次比较两个相邻的元素,将较大的元素交换至右端
*
* @param a
* @return
*/
public static int[] bubblingSort(int[] a) {
if (a == null) return null;
for (int i = 0; i < a.length - 1; i++) {//最后一个元素不要排序了、所以减1、少排序一轮
for (int j = 0; j < a.length - 1 - i; j++) { //进行到第几轮就要少几次移动交换(因为每循环一次最末尾或者最前面的是最大、最小的),所以减i次
if (a[j] > a[j + 1]) {//从小到大排序
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
return a;
}
/**
* 选择排序
*
* @param a
* @return
*/
public static int[] selectSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) { //最后一个不再排序了
int minIndex = i;//找到每一轮最小值
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");
return a;
}
/**
* 插入排序
*
* @param a
* @return
*/
public static int[] insertSort(int[] a) {
for (int i = 1; i < a.length; i++) {//默认第0个是有序的
for (int j = i; j > 0; j--) { //每次从右边数组增加一个数与左边数据判断、交换位置(左边数据进行排序)
if (a[j] < a[j - 1]) {
int temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}
for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");
return a;
}
}