第一章 引论

1.1 编写一个程序解决选择问题。令k=N/2,画出表格显示程序对于N种不同的值的运行时间。

package com.example.demo.entity;
import java.util.Arrays;
import java.util.Random;

public class ChoiceMatter {
    public static void main(String[] args) {
        //数组长度设置
        int n = 100000;
        int k = n / 2;
        //方法一:将原数组直接排序,然后输出第k个数
        int[] arr = ChoiceMatter.createArr(n);
        long t1 = System.currentTimeMillis();
        int[] a  = ChoiceMatter.sortArr(arr);
        System.out.println(a[k]);
        long t2 = System.currentTimeMillis() ;
        System.out.println(t2-t1);

        //方法二
        int[] arr1 = ChoiceMatter.createArr(n);
        long t3 = System.currentTimeMillis();
        int[] b = Arrays.copyOf(arr1,k);
        sortArr(b);
        for (int i = k;i < arr.length; i++){
            if(arr1[i] > b[k-1]){
                b[k-1] = b[k-2];
                for(int j=k-2;j>0;j--){
                    if (arr1[i] < b[j]){
                       b[j+1] = arr[i];
                       break;
                    }else{
                        b[j] = b[j-1];
                    }
                }
            }
        }
        System.out.println(b[k-1]);
        long t4 = System.currentTimeMillis();
        System.out.println(t4-t3);
    }
    //生成随机数组
    public static int[] createArr(int n){
        Random random = new Random(100);
        int[] arr = new int[n];
        for (int i=0; i < n; i++){
            arr[i] = random.nextInt();
        }
        return  arr;
    }
    //冒泡排序
    public static int[] sortArr(int[] arr){
        for (int i=1;i

 

1.2编写一个程序求解字谜游戏问题

 

import java.util.Random;

//字谜问题
public class Character {
    
    public static final Random RANDOM = new Random(47);
    
    public static final String[] WORDS = new String[]{"ynz","yzgm","oqz","owznt","z"};
    
    public static void main(String[] args) {
        char[][] chars = createTable(5);
        printTable(chars);
        findWord(chars);
    }
    //按照标量方向寻找满足的单词(或者说字符串)
    public static void findWord(char[][] chars){
        long start = System.currentTimeMillis();
        for (int i = 0; i < chars.length; i++) {
            for (int j = 0; j < chars.length; j++) {
                for (int k = 0; k < chars.length; k++) {
                    for (int l = 0; l < chars.length; l++) {
                        if (i == k && j == l) {
                            printWord(String.valueOf(chars[i][j]), i, j, k, l);
                            continue;
                        }
                        if (k != i && j != l && (k - i) != (j - l) && (k - i) != (l - j)) {
                            continue;
                        }
                        StringBuffer stringBuffer = new StringBuffer();
                        if (i == k) {
                            if (j > l) {
                                for (int m = j; m >= l; m--) {
                                    stringBuffer.append(chars[i][m]);
                                }
                            }else {
                                for (int m = j; m <= l; m++) {
                                    stringBuffer.append(chars[i][m]);
                                }
                            }
                        }
                        if (j == l) {
                            if (i > k) {
                                for (int m = i; m >= k; m--) {
                                    stringBuffer.append(chars[m][j]);
                                }
                            }else {
                                for (int m = i; m <= k; m++) {
                                    stringBuffer.append(chars[m][j]);
                                }
                            }
                        }
                        if ((k - i) == (j - l)) {
                            if (i > k) {
                                for (int m = i,n = j; m >= k && n <= l; m--,n++) {
                                    stringBuffer.append(chars[m][n]);
                                }
                            }else {
                                for (int m = i,n = j; m <= k && n >= l; m++,n--) {
                                    stringBuffer.append(chars[m][n]);
                                }
                            }
                        }
                        if ((k - i) == (l - j)) {
                            if (i > k) {
                                for (int m = i,n = j; m >= k && n >= l; m--,n--) {
                                    stringBuffer.append(chars[m][n]);
                                }
                            }else {
                                for (int m = i,n = j; m <= k && n <= l; m++,n++) {
                                    stringBuffer.append(chars[m][n]);
                                }
                            }
                        }
                        printWord(stringBuffer.toString(), i, j, k, l);
                    }
                }
            }
        }
        System.out.println("-------------------------------------------------");
        System.out.println("cost time:" + (System.currentTimeMillis() - start) + "ms");
    }
    //判断是否是既定的一个单词(或字符串)并打印
    public static void printWord(String word,int i ,int j,int k,int l){
        for (int m = 0; m < WORDS.length; m++) {
            if (word.equals(WORDS[m])) {
                System.out.println("find word:" + WORDS[m]);
                System.out.println("scalar:" + "[" + (i+1) + "," + (j+1) + "]->[" + (k+1) + "," + (l+1) + "]");
            }
        }
    }
    //创建随即字符二维数组
    public static char[][] createTable(int length){
        char[][] chars = new char[length][length];
        for (int i = 0; i < chars.length; i++) {
            for (int j = 0; j < chars[i].length; j++) {
                chars[i][j] = (char)(97 + RANDOM.nextInt(26));
            }
        }
        return chars;
    }
    //打印二维数组
    public static void printTable(char[][] chars){
        System.out.println("---------------------------------------------");
        for (int i = 0; i < chars.length; i++) {
            System.out.print("\t" + (i+1));
        }
        System.out.println();
        for (int i = 0; i < chars.length; i++) {
            System.out.print((i+1));
            for (int j = 0; j < chars.length; j++) {
                System.out.print("\t" + chars[i][j]);
            }
            System.out.println();
        }
        System.out.println("---------------------------------------------");
    }
    
}

 

你可能感兴趣的:(数据结构与算法分析,练习)