练习
for循环
-
// 1!+2!+...+10! 的和
// 1!+2!+...+10! 的和
package com.zhou.java.demo01;
public class demo05 {
// 1!+2!+...+10! 的和
public static void main(String[] args) {
long sum = 0;
for (int i = 1; i <= 10; i++) {
int t = 1;
for (int j = 1; j <= i; j++) {
t *= j;
}
sum += t;
}
System.out.println(sum);
}
}
-
杨辉三角
package com.zhou.java.demo01;
public class demo06 {
public static void main(String[] args) {
// 杨辉三角
int[][] arr = new int[10][];
for (int i = 0; i < arr.length; i++) {
arr[i] = new int[i + 1];
for (int j = 0; j < arr[i].length; j++) {
if (j == 0) {
arr[i][j] = 1;
} else if (j == arr[i].length - 1) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
}
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
-
把一个数放到一个数组内,并按大小找到自己相应的位置
package com.zhou.java.demo01;
public class demo07 {
public static void main(String[] args) {
int[] arr={1,3,4,5,7};
int num=6;
int index=-1;
for (int i = 0; i < arr.length; i++) {
if (num<=arr[i]){
index=i;
break;
}else {
index= arr.length;
}
}
System.out.println(index);
int[] arr2=new int[arr.length+1];
for (int i=0,j=0;i< arr2.length;i++){
if (i!=index){
arr2[i]=arr[j];
j++;
}else {
arr2[i]=num;
}
}
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i]+" ");
}
}
}
-
九九乘法表
package com.zhou.java.demo01;
public class dome02 {
public static void main(String[] args) {
int result=0;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
result=i*j;
System.out.print(j+"*"+i+"="+result+"\t");
}
System.out.println();
}
}
}
冒泡排序
package com.zhou.java.demo01;
// 冒泡排序
public class dome03 {
public static void main(String[] args) {
int[] arr = {1, 5, 2, 3, 9, 6, 4};
int[] sort = sort(arr);
for (int i : sort) {
System.out.print(i + " ");
}
}
public static int[] sort(int[] array) {
int temp;
for (int i = 0; i < array.length - 1; i++) {
boolean flag = false;
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j + 1] > array[j]) {
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
flag = true;
}
}
if (flag == false) {
break;
}
}
return array;
}
}
稀疏数组
package com.zhou.java.demo01;
public class dome04 {
public static void main(String[] args) {
int[][] arr = new int[11][11];
arr[1][3] = 2;
arr[3][5] = 5;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println("============================================");
// 稀疏数组
int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] != 0) {
sum++;
}
}
}
System.out.println("sum=" + sum);
System.out.println("=========================================================");
int[][] arr1 = new int[sum + 1][3];
arr1[0][0] = 11;
arr1[0][1] = 11;
arr1[0][2] = sum;
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] != 0) {
count++;
arr1[count][0] = i;
arr1[count][1] = j;
arr1[count][2] = arr[i][j];
}
}
}
System.out.println("稀疏数组");
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i][0] + "\t"
+ arr1[i][1] + "\t"
+ arr1[i][2] + "\t");
}
System.out.println("=======================================");
System.out.println("还原稀疏数组");
int[][] arr2 = new int[arr1[0][0]][arr1[0][1]];
for (int i = 1; i < arr1.length; i++) {
arr2[arr1[i][0]][arr1[i][1]] = arr1[i][2];
}
for (int[] ints : arr2) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
}
}
递归
-
阶乘
package com.zhou.java.demo01;
public class demo01 {
public static void main(String[] args) {
System.out.println(f(3));
}
//递归
public static double f(double n){
if (n==1){
return n;
}else {
return n*f(n-1);
}
}
}
-
走迷宫
package com.zhou.java.demo02;
public class Demo01 {
public static void main(String[] args) {
int[][] arr = new int[12][12];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][0] = 1;
arr[0][j] = 1;
arr[arr.length - 1][j] = 1;
arr[i][arr.length - 1] = 1;
arr[3][1] = 1;
arr[3][2] = 1;
arr[2][2] = 1;
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
finWay(arr, 1, 1);
System.out.println("==============================================");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static boolean finWay(int[][] map, int i, int j) {
if (map[10][10] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (finWay(map, i + 1, j)) {
return true; //向下
} else if (finWay(map, i, j + 1)) {
return true; //向右
} else if (finWay(map, i - 1, j)) {
return true; //向上
} else if (finWay(map, i, j - 1)) {
return true; //向左
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
}
-
汉罗塔
package com.zhou.java.demo02;
public class Demo02 {
// 汉罗塔
public static void main(String[] args) {
Tower tower = new Tower();
tower.move(3,'A','B','C');
}
static class Tower{
public void move(int num,char a,char b,char c){
if (num==1){
System.out.println(a+"——>"+b);
}else {
move(num-1,a,c,b);
System.out.println(a+"——>"+c);
move(num-1,b,a,c);
}
}
}
}
-
皇后问题
package com.zhou.java.demo02;
public class Demo03 {
// 几个皇后
int max=8;
//数组array,保存皇后位置 arr = {0,4,7,5,2,6,1,3}
int[] array = new int[max];
//统计解法
static int count = 0;
public static void main(String[] args) {
Demo03 queen8 = new Demo03();
queen8.check(0);
System.out.println("解法:" + count);
//随机拿出一组,复原 比如0 就是 第一行,第一列,4就是第二行的第五列
int[] arr = new int[]{0, 4, 7, 5, 2, 6, 1, 3};
int[][] queenArr = new int[8][8];
//初始化棋盘
for (int[] data : queenArr) {
for (int item : data) {
System.out.printf("%4d", item);
}
System.out.println();
}
for (int i = 0; i < arr.length; i++) {
queenArr[i][arr[i]] = 1;
}
System.out.println("===============================");
//放置皇后的位置设置为1
for (int[] data : queenArr) {
for (int item : data) {
System.out.printf("%4d", item);
}
System.out.println();
}
}
/**
* 查看当我们放置第n个皇后,检测该皇后是否和前面已经摆放的冲突
*
* @param n 第几个皇后
* @return
*/
private boolean judge(int n) {
for (int i = 0; i < n; i++) {
//array[i] == array[n] 同一列 , Math.abs(n - i) == Math.abs(array[n] - array[i]) 同一斜线
// n = 1 ==>第二个皇后,放在第二列 n=1 array[1]=1 。
// n-i 理解为x坐标,也就是数组索引下标为x,数组的值是y,当这两个值相等,斜率就是1,所以在一个斜线上
//Math.abs(1 - 0) = 1 Math.abs(1 - 0) = 1
if (array[i] == array[n] || Math.abs(n - i) == Math.abs(array[n] - array[i])) {
return false;
}
}
return true;
}
/**
* 放置皇后
* check没依次递归的时候,进入到check方法都有一套for循环,因此会有回溯。相当于8个栈,每个栈有一个check方法
*
* @param n
*/
public void check(int n) {
if (n == max) { //8个皇后已经放置完成
print();
return;
}
//依次放入皇后,并判断
for (int i = 0; i < max; i++) {
//先把当前皇后 放到该行的第1列
array[n] = i;
//判断放放置第n个皇后后到i列时候,是否冲突
if (judge(n)) {
//接着放n+1个皇后
check(n + 1);
}
//若冲突,就继续执行array[n] == i ,即将第n个皇后,放置在本行的后移的一个位置。
}
}
//将皇后位置输出
private void print() {
count++;
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}