1、一、实验题目:将 n 个集装箱装上载重量为 c1 和 c2 的轮船,其中集装箱总重量 nb e s t x j = x j ;b e s t w = c w ;r e t u r n ;r - = w i ;c w + w i b e s t wx i = 0 ;b a c k t r a c e ( i +1 ) ;r + = w i ;YNYNYNp u b l i c i n t m a x L o a d i n g ( i n t w w , i n t c c , i n t x x )Yl o a d . m a x L o a d i n g ( w , c ,x ) ;NNs
2、 t a t i c i n t n ;s t a t i c i n t w ;s t a t i c i n t c ;s t a t i c i n t c w ;s t a t i c i n t b e s t w ;s t a t i c i n t r ;s t a t i c i n t x ;s t a t i c i n t b e s t x ;N算法设计与分析实验报告2三、实验结果:input.txtoutput.txt四、源程序:package javaapplication1;import java.io.*;public class Main static int
3、 n;static int w;static int c1,c2;static int cw;static int bestw;static int r;static int x;static int bestx;算法设计与分析实验报告3public static int maxloading(intww,int cc,intxx)w=ww;c1=cc;bestw=0;cw=0;x=new intn+1;bestx=xx;r=0;for(int i=1;in) if(cwbestw)for(int j=1;jbestw)xi=0;backtrack(i+1); r+=wi; public st
4、atic void main(String args) throws IOException BufferedReader read =new BufferedReader(new InputStreamReader(new FileInputStream(“input.txt“);String a=new String();a=read.readLine();n=Integer.parseInt(a);System.out.println(“集装箱个数: “+n);x=new intn+1;Stringb=new Stringn;a=read.readLine();System.out.pr
5、intln(“集装箱重量: “+a);算法设计与分析实验报告4b=a.split(“,“);w=new intn+1;for(int i=1;iwj)temp=wi;wi=wj;wj=temp;if(w3c1)elseSystem.out.println(“轮船 1 装载的集装箱:“);for (int u=1;u(result+c2)System.out.println(“轮船 1 可装:“+result+“ “+“轮船 2 装不完.“);elseSystem.out.println(“轮船 2 装载的集装箱:“);for (int u=1;uc1)算法设计与分析实验报告5elseprint.println(“轮船 1 装载的集装箱:“);for (int u=1;u(result+c2)print.println(“轮船 1 可装:“+result+“ “+“轮船 2 装不完。“);elseprint.println(“轮船 2 装载的集装箱:“);for (int u=1;un+1;u+)if(bestxu=0)print.println(u+“ “);print.println(“最优装载-轮船 1:“+result+“ “+“轮船 2:“+(r-result);read.close();print.close();