




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、南京信息工程大學 算法設計與分析 實驗(實習)報告實驗(實習)名稱 實驗(實習)日期 得分 指導老師 系 專業(yè) 班級 姓名 學號 一、 實驗目的:能夠熟悉貪心算法和最優(yōu)裝載算法二、 實驗內(nèi)容 貪心算法,背包問題package xd;import java.util.Arrays;import java.util.Scanner;public class XD public static void main(String args)Scanner in = new Scanner(System.in);System.out.println("Please enter the numbe
2、r of objects(請輸入物品的數(shù)量:)");int n = in.nextInt(); int w = new intn; int v = new intn; System.out.println("Now please enter the weight of the objects(現(xiàn)在請輸入這些物品的重量:)");for(int i=0;i<n;i+)wi = in.nextInt();System.out.println("Now please enter the value of the ojects(現(xiàn)在請輸入這些物品的價值:)&
3、quot;);for(int j=0;j<n;j+)vj = in.nextInt();System.out.println("Now please enter the capacity of the pack(請輸入背包的容積:)");int c = in.nextInt();double startTime = System.currentTimeMillis();int index = new intn;double r = new doublen; for(int k=0;k<n;k+)rk = vk/wk; indexk = k;double temp
4、 = 0;int x = 0;for(int i=0;i<n-1;i+)for(int j=i+1;j<n;j+)if(ri<rj)temp = ri;ri = rj;rj = temp;x = indexi;indexi = indexj;indexj = x;int w1 = new intn;int v1 = new intn;for(int i=0;i<n;i+)v1i = vindexi;w1i = windexi;int x1 = new intn; for (int i = 0; i < n; i+) x1i = 0; for(int i=0;i&l
5、t;n;i+)if(w1i<c)x1i = 1;c = c-w1i;System.out.println("The solution vector is(解向量是:)"+Arrays.toString(x1);int maxValue=0;for(int i=0;i<n;i+)if(x1i=1)maxValue = maxValue+v1i;System.out.println("Now,the max value of object in the pack is(背包可以容納的最大價值是:)"+maxValue);double endTim
6、e = System.currentTimeMillis();System.out.println("The Statements takes the time is(算法執(zhí)行所花費的時間為:)"+(endTime-startTime)+ " milliseconds!");最優(yōu)裝載import javax.swing.*;import java.io.*;import java.util.*;public class MaxLoading public static void main(String args) String s1=JOptionPan
7、e.showInputDialog(null,"請輸入貨物數(shù)量:", "最優(yōu)裝載",JOptionPane.QUESTION_MESSAGE); n=Integer.parseInt(s1); w=new int n; System.out.println("請輸出貨物的重量數(shù)組:"); for(int i=0;i<n;i+)wi=(int)(100*Math.random();System.out.println(wi); String s2=JOptionPane.showInputDialog(null,"請輸入
8、第一艘輪船的載重量:", "最優(yōu)裝載",JOptionPane.QUESTION_MESSAGE); c=Integer.parseInt(s2); maxLoading(w,c,x); System.out.print("輸出當前最優(yōu)解:"); for(int i=1;i<n+1;i+)System.out.print(xi+" "); System.out.println(); System.out.println("最優(yōu)解:"+bestw); static int n; static int
9、w; static int c; static int cw; static int bestw; static int r; static int x; static int bestx; public static int maxLoading(intww,int cc,intxx) n=ww.length-1; w=ww; c=cc; bestw=0; x=new int n+1; bestx=xx; for(int i=1;i<=n;i+) r+=wi; backtrack(1); return bestw; public static void backtrack(int i) if(i>n) if(cw>bestw) for(int j=1;j<
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 四川園林古建景觀施工方案
- 生物防輻射技術協(xié)議
- 第二單元圖像處理的基本方法第4課三、《制作一個交通標志》教學設計 2023-2024學年人教版初中信息技術七年級下冊
- 宇宙輻射防護材料供應協(xié)議
- Unit 3 Our Hobbies Topic 3 Section B 教學設計 2024-2025學年仁愛科普版八年級英語上冊
- 船舶制造行業(yè)船舶設計圖紙轉(zhuǎn)讓協(xié)議范本
- 幼兒音樂教學計劃
- 人教版歷史與社會九年級上冊1.1.2第一次世界大戰(zhàn) 教學設計
- 2025至2030年中國奧復康注射液數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國壁架配件數(shù)據(jù)監(jiān)測研究報告
- 網(wǎng)絡傳播概論(彭蘭第5版) 課件全套 第1-8章 網(wǎng)絡媒介的演變-網(wǎng)絡傳播中的“數(shù)字鴻溝”
- 4.1.1 有理數(shù)指數(shù)冪-參考課件
- 雷達簡介講解課件
- 人教版六年級數(shù)學下冊全冊大單元教學任務單
- JJF(新) 112-2023 微量殘?zhí)繙y定儀校準規(guī)范
- 超載限位器調(diào)試報告
- 2024銷售人員年終工作總結2篇
- 2024年牛排行業(yè)分析報告及未來發(fā)展趨勢
- 智能微電網(wǎng)應用技術
- 車間維修現(xiàn)場安全操作規(guī)程范文
- 【全套】醫(yī)院智能化系統(tǒng)報價清單
評論
0/150
提交評論