⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 01背包四种算法实现(java版)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(40, 40, 40)
                        .add(jButton1))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 41, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4)
                    .add(jButton2))
                .add(6, 6, 6)
                .add(jLabel10)
                .add(16, 16, 16)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(22, 22, 22)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel21)
                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton4))
                .add(2, 2, 2)
                .add(jLabel5)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel11)
                            .add(jLabel12))
                        .add(17, 17, 17)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel6)
                            .add(jLabel13)
                            .add(jLabel14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(35, 35, 35)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel7)
                            .add(jLabel15)
                            .add(jLabel16))
                        .add(35, 35, 35)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel8)
                            .add(jLabel18)
                            .add(jLabel17))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 32, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel9)
                            .add(jLabel19)
                            .add(jLabel20))
                        .add(21, 21, 21))
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 185, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO 将在此处添加您的处理代码:
                                          this.jTextArea1.setText("");
                                    String BagWeight = this.jTextField1.getText();
                                    String BagValuet = this.jTextField2.getText();
                                    String capcity = this.jTextField3.getText();
                                    if (BagWeight != null && BagValuet != null
							&& capcity != null) {
                                                 // 物品重量转为双精度数
						StringTokenizer st = new StringTokenizer(BagWeight);
						ArrayList al = new ArrayList();
						while (st.hasMoreTokens()) {
							Double d = new Double(st.nextToken());
							al.add(d);
						}
						double weight[] = new double[al.size()];
						for (int i = 0; i < al.size(); i++) {
							Double temp = (Double) al.get(i);
							weight[i] = temp.doubleValue();
						}
                                                
						// 物品价值转为双精度数
						StringTokenizer st2 = new StringTokenizer(BagValuet);
						ArrayList al2 = new ArrayList();
						while (st2.hasMoreTokens()) {
							Double d = new Double(st2.nextToken());
							al2.add(d);
						}
						double value[] = new double[al2.size()];
						for (int i = 0; i < al2.size(); i++) {
							Double temp = (Double) al2.get(i);
							value[i] = temp.doubleValue();
						}
                                                // 背包容量转为整数
						double c = new Double(capcity).doubleValue();
						// 最大价值及最优解
						//int m[][] = new int[weight.length][c + 1];
						//int x[] = new int[weight.length];
						// 得到最优值和最优解
                                            double maxAnswer =   BackTrace.knapsack(value,weight,c);
                                            System.out.println(maxAnswer);
                                                //算法运行过程:
                                                /*StringBuffer sbf = new StringBuffer("最优解二进制表示:");
                                                for (int k = 0; k < x.length; k++) {
							sbf.append(x[k] + "    ");
						}
                                                sbf.append("\n");
                                                //显示最大值和最优值
                                                StringBuffer sb = new StringBuffer();
						int MaxV = 0;
                                                int CurrentC =c;
                                                
						for (int k = 0; k < x.length; k++) {
							if (x[k] != 0) {
								int t = k+1;
								sb.append("第" + t + "个   ");
								MaxV = MaxV + value[k];
                                                                CurrentC = CurrentC - weight[k];
                                                                sbf.append("选择了第" + t + "个当前价值为"+MaxV+
                                                                        "剩余容量"+ CurrentC);
                                                                 sbf.append("\n");
							}
						}
                                                this.jTextArea1.setText(sbf.toString());
                                                this.jLabel19.setText(new Integer(MaxV)
								.toString());
                                                this.jLabel20.setText(sb.toString());*/
                                    }
        
        
  
					
        
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO 将在此处添加您的处理代码:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
                                     this.jTextArea1.setText("");
                                    String BagWeight = this.jTextField1.getText();
                                    String BagValuet = this.jTextField2.getText();
                                    String capcity = this.jTextField3.getText();
                                    if (BagWeight != null && BagValuet != null
							&& capcity != null) {
                                                 // 物品重量转为整数
						StringTokenizer st = new StringTokenizer(BagWeight);
						ArrayList al = new ArrayList();
						while (st.hasMoreTokens()) {
							Integer integer = new Integer(st.nextToken());
							al.add(integer);
						}
						int weight[] = new int[al.size()];
						for (int i = 0; i < al.size(); i++) {
							Integer temp = (Integer) al.get(i);
							weight[i] = temp.intValue();
						}
                                                
						// 物品价值转为整数
						StringTokenizer st2 = new StringTokenizer(BagValuet);
						ArrayList al2 = new ArrayList();
						while (st2.hasMoreTokens()) {
							Integer integer = new Integer(st2.nextToken());
							al2.add(integer);
						}
						int value[] = new int[al2.size()];
						for (int i = 0; i < al2.size(); i++) {
							Integer temp = (Integer) al2.get(i);
							value[i] = temp.intValue();
						}
                                                // 背包容量转为整数
						int c = new Integer(capcity).intValue();
						// 最大价值及最优解
						int m[][] = new int[weight.length][c + 1];
						int x[] = new int[weight.length];
						// 得到最优值和最优解
                                                Dynamic_Programming.knapsack(value, weight, c, m);
						Dynamic_Programming.traceback(m, weight, c, x);
                                                //算法运行过程:
                                                StringBuffer sbf = new StringBuffer();
                                                //显示最大值和最优值
                                                StringBuffer sb = new StringBuffer();
						int MaxV = 0;
                                                int CurrentC =c;
						for (int k = 0; k < x.length; k++) {
							if (x[k] != 0) {
								int t = k+1;
								sb.append("第" + t + "个   ");
								MaxV = MaxV + value[k];
                                                                CurrentC = CurrentC - weight[k];
                                                                sbf.append("选择了第" + t + "个当前价值为"+MaxV+
                                                                        "剩余容量"+ CurrentC);
                                                                 sbf.append("\n");
							}
                                                         
						}
                                                this.jTextArea1.setText(sbf.toString());
                                                this.jLabel17.setText(new Integer(MaxV)
								.toString());
                                                this.jLabel18.setText(sb.toString());
                                    }
        
        
  
					
        
    }//GEN-LAST:event_jButton3ActionPerformed

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -