算法(JAVA)----兩道小小課后題


         LZ最近翻了翻JAVA版的數據結構與算法,無聊之下將書中的課后題一一給做了一遍,在此給出書中課后題的答案(非標准答案,是LZ的答案,猿友們可以貢獻出自己更快的算法)。

 

1、編寫一個程序解決選擇問題。令k=N/2,畫出表格顯示程序對於N種不同的值的運行時間。

 

         分析:選擇問題是指從N個數當中,按升序(降序也可以)排列,找出第k個數。LZ的寫法是采用書中給出的算法自己實現的,分別采用冒泡排序和分批處理的方式。以下為LZ寫出的算法代碼。

import java.util.Arrays; import java.util.Random; //選擇問題答案
public class Select { public static final Random RANDOM = new Random(47); //假設N = 10
    public static void main(String[] args) { for (int i = 0; i < 10; i++) { printResult(createArray(RANDOM.nextInt(100000))); } } //冒泡排序
    public static void sort(int[] values){ for (int i = 1; i < values.length; i++) { for (int j = 0; j < values.length - i; j++) { if (values[j] > values[j + 1]) { int temp = values[j]; values[j] = values[j + 1]; values[j + 1] = temp; } } } } //分批處理
    public static int select(int[] values){ if (values == null || values.length == 0) { throw new NullPointerException("values can't be null."); } int k = values.length / 2; int[] temp = Arrays.copyOf(values, k); sort(temp); for (int i = k ; i < values.length; i++) { if (values[i] < temp[k - 1]) { temp[k - 1] = temp[k - 2]; for (int j = k - 2; j >0; j--) { if (values[i] > temp[j]) { temp[j + 1] = values[i]; break; }else { temp[j] = temp[j - 1]; } } } } return temp[k - 1]; } //創建隨即數組
    public static int[] createArray(int length){ int[] values = new int[length]; for (int i = 0; i < length; i++) { values[i] = RANDOM.nextInt(length * 2); } return values; } //打印結果
    public static void printResult(int[] values){ System.out.println("length:" + values.length); long start = System.currentTimeMillis(); System.out.println("result:" + select(values)); System.out.println("cost:" + (System.currentTimeMillis() - start) + "ms"); System.out.println("--------------------------------"); } }

 

2、編寫一個程序求解字謎游戲問題。

 

          分析:字謎游戲是從一個二維的字符數組中按照標量尋找單詞的過程,LZ寫的算法就是最直觀的算法,有很多嵌套循環,倘若字符表很大的時候,這種方式會很慢。想必書中的后面章節應該會有更簡單的方式,只不過LZ還沒看到,以后有機會改善這個算法。

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("---------------------------------------------"); } }

 

結束語

 

                小算怡情,大算傷身。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM