




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、算法設(shè)計與分析實驗報告實驗序號: 實驗項目名稱:編程實現(xiàn)動態(tài)規(guī)劃的算法學號姓名 專業(yè)、班 11軟服2班實驗地點指導教師實驗時間2013/11/29一、實驗目的及要求 1. 體驗實現(xiàn)程序的運行過程 2. 寫出源程序,并編譯運行。2、 實驗內(nèi)容與步驟 1.矩陣連乘問題(或多邊形游戲問題) 2.最長公共子序列問題(或最接近點對問題)3、 實驗方法 4、 實驗結(jié)果與數(shù)據(jù)處理最長公共子序列:乘法五、分析與討論對上機實踐結(jié)果進行分析,上機的心得體會。六、教師評語簽名:日期:附源程序清單: 最長公共子序列:Import java.io.BufferedReader; Import java.io.IOExc
2、eption; Import java.io.InputStreamReader; Import java.util.ArrayList; Import java.util.Scanner; Import java.util.List; /* * 動態(tài)規(guī)劃法解最長公共子系列。 * author藍冠恒 */Public class LcsLength publicstatic List<Character> resultList = New ArrayList<Character>(); /* * 計算最優(yōu)值 * param x * 字符系列數(shù)組 * param y *
3、字符系列數(shù)組 * param c * 存儲x和y最長公共子系列長度數(shù)組 * param b * 記錄c中元素對應子問題的解的數(shù)組 */ public static void lcsLength(char x, char y, int c, int b) int m = x.length - 1; int n = y.length - 1; resultList.clear(); for (int i = 1; i <= m; i+) ci0 = 0; for (int i = 1; i <= n; i+) c0i = 0; for (int i = 1; i <= m; i+
4、) for (int j = 1; j <= n; j+) if (xi = yj) cij = ci - 1j - 1 + 1; bij = 1; else if (ci - 1j >= cij - 1) cij = ci - 1j; bij = 2; else cij = cij - 1; bij = 3; public static void lcs(int i, int j, char x, int b) if (i = 0 | j = 0) return; if (bij = 1) lcs(i - 1, j - 1, x, b); resultList.add(xi);
5、else if (bij = 2) lcs(i - 1, j, x, b); else lcs(i, j - 1, x, b); publicstaticvoid main(String arg) String a; String input; char x; char y; Scanner scan= New Scanner(System.in); BufferedReader in = New BufferedReader(New InputStreamReader(System.in); do try do System.out.println("請輸入第一串字符系列"
6、;); input = in.readLine().trim(); while(input.equals(""); input = "S" + input; x = input.toCharArray(); do System.out.println("請輸入第二串字符系列"); input = in.readLine().trim(); while (input.equals(""); input = "S" + input; y = input.toCharArray(); int b =
7、newintx.lengthy.length;int c = New intx.lengthy.length; lcsLength(x, y, c, b);/ 計算最優(yōu)值lcs(x.length- 1, y.length- 1, x, b);/ 構(gòu)造最長公共子系列int size = resultList.size(); System.out.print("最長公共子系列為:"); for(int i = 0; i < size; i+) System.out.print(resultList.get(i); System.out.println("n&qu
8、ot;); catch (IOException e) e.printStackTrace(); System.out.print("繼續(xù)輸入請按y。退出請按任意鍵!"); a=scan.nextLine(); while (a.equals("y"); 連乘問題 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Scanner;
9、import java.util.List; /* * 動態(tài)規(guī)劃法解最長公共子系列。 * author 藍冠恒 */ public class LcsLength public static List<Character> resultList = new ArrayList<Character>(); /* * 計算最優(yōu)值 * param x * 字符系列數(shù)組 * param y * 字符系列數(shù)組 * param c * 存儲x和y最長公共子系列長度數(shù)組 * param b * 記錄c中元素對應子問題的解的數(shù)組 */ public static void lcsLen
10、gth(char x, char y, int c, int b) int m = x.length - 1; int n = y.length - 1; resultList.clear(); for (int i = 1; i <= m; i+) ci0 = 0; for (int i = 1; i <= n; i+) c0i = 0; for (int i = 1; i <= m; i+) for (int j = 1; j <= n; j+) if (xi = yj) cij = ci - 1j - 1 + 1; bij = 1; else if (ci - 1
11、j >= cij - 1) cij = ci - 1j; bij = 2; else cij = cij - 1; bij = 3; public static void lcs(int i, int j, char x, int b) if (i = 0 | j = 0) return; if (bij = 1) lcs(i - 1, j - 1, x, b); resultList.add(xi); else if (bij = 2) lcs(i - 1, j, x, b); else lcs(i, j - 1, x, b); public static void main(Stri
12、ng arg) String a; String input; char x; char y; Scanner scan= new Scanner(System.in); BufferedReader in = new BufferedReader(new InputStreamReader(System.in); do try do System.out.println("請輸入第一串字符系列"); input = in.readLine().trim(); while (input.equals(""); input = "S"
13、+ input; x = input.toCharArray(); do System.out.println("請輸入第二串字符系列"); input = in.readLine().trim(); while (input.equals(""); input = "S" + input; y = input.toCharArray(); int b = new intx.lengthy.length; int c = new intx.lengthy.length; lcsLength(x, y, c, b);/ 計算最優(yōu)值 lcs(x.length- 1, y.length- 1, x, b);/ 構(gòu)造最長公共子系列int size = resultList.size(); System.out.print("最長公共子系列為:"); for (int i = 0; i < size; i+) System.out.print
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度高級管理人員競業(yè)禁止合同
- 農(nóng)業(yè)生產(chǎn)資金投入與財務管理手冊
- 開幕式致辭與未來發(fā)展展望報告
- 員工年終工作總結(jié)報告模板集萃
- 互聯(lián)網(wǎng)廣告投放及推廣合作協(xié)議
- 農(nóng)業(yè)生產(chǎn)投入品減量增效技術(shù)指導手冊
- 農(nóng)業(yè)產(chǎn)業(yè)扶貧政策及項目申報指導手冊
- 智能家居技術(shù)研發(fā)推廣合作協(xié)議
- 健身房客戶服務手冊
- 健身房健身器材租賃合同
- 2.0MWp屋頂太陽能分布式光伏發(fā)電項目監(jiān)理大綱2
- 小學夢想開《去遠方》教學設(shè)計
- Q∕SY 06349-2019 油氣輸送管道線路工程施工技術(shù)規(guī)范
- CEO自戀及其經(jīng)濟后果研究:以格力電器為例
- 紅土鎳礦濕法冶煉技術(shù)綜述
- 六鑫伺服刀塔說明書LS系列
- 19.骨折術(shù)后內(nèi)固定取出臨床路徑
- 水利水電工程金屬結(jié)構(gòu)與機電設(shè)備安裝安全技術(shù)規(guī)程
- 腎內(nèi)科臨床診療規(guī)范(南方醫(yī)院)
- 珍愛生命 安全第一 中小學主題教育班會
- 二十八星宿(課堂PPT)
評論
0/150
提交評論