這兩天看高程中的演算法,把其中的遞迴揹包問題用java執行了。特請高手們指點!
/*
* Created on Apr 2, 2005
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package com.kevin.datastru;
/**
* @author seemee
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
/*import java.io.BufferedReader;
import java.io.InputStreamReader;*/
import java.io.IOException;
public class CarryBag {
double limitW, totV, maxv;
int option[] = new int[100], cop[] = new int[100];
Struct a[]= new Struct[100];
int n;
public void find(int i, double tw, double tv){
int k;
if ( tw + a.weight <= limitW){
cop=1;
if(i<n-1){
find(i+1, tw+a.weight, tv);
}
else {
for(k=0;k<n;k++){
option[k] = cop[k];
}
maxv = tv;
}
cop = 0;
}
if (tv - a.value > maxv) {
if (i < n-1) {
find(i+1, tw, tv - a.value);
}
else {
for (k = 0; k<n; k++){
option[k] = cop[k];
}
maxv = tv - a.value;
}
}
}
public static void main(String[] args) throws IOException{
int k;
CarryBag cBag = new CarryBag();
cBag.a[0] = cBag.new Struct(5.0, 4.0);
cBag.a[1] = cBag.new Struct (3.0, 4.0);
cBag.a[2] = cBag.new Struct (2.0, 3.0);
cBag.a[3] = cBag.new Struct (1.0, 1.0);
cBag.limitW = 7.0;
cBag.maxv = 0.0;
cBag.totV = 12.0;
cBag.n = 4;
for (k = 0; k < cBag.n ; k++) {
cBag.cop[k] = 0;
}
cBag.find(0, 0.0, cBag.totV);
for(k = 0; k < cBag.n ; k++){
if(cBag.option[k] != 0) {
System.out.println(k);
}
}
System.out.println(cBag.maxv);
}
class Struct{
double weight;
double value;
Struct (double weight, double value){
this.weight = weight;
this.value = value;
}
}
}
以上程式碼複製、貼上即可執行!
* Created on Apr 2, 2005
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package com.kevin.datastru;
/**
* @author seemee
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
/*import java.io.BufferedReader;
import java.io.InputStreamReader;*/
import java.io.IOException;
public class CarryBag {
double limitW, totV, maxv;
int option[] = new int[100], cop[] = new int[100];
Struct a[]= new Struct[100];
int n;
public void find(int i, double tw, double tv){
int k;
if ( tw + a.weight <= limitW){
cop=1;
if(i<n-1){
find(i+1, tw+a.weight, tv);
}
else {
for(k=0;k<n;k++){
option[k] = cop[k];
}
maxv = tv;
}
cop = 0;
}
if (tv - a.value > maxv) {
if (i < n-1) {
find(i+1, tw, tv - a.value);
}
else {
for (k = 0; k<n; k++){
option[k] = cop[k];
}
maxv = tv - a.value;
}
}
}
public static void main(String[] args) throws IOException{
int k;
CarryBag cBag = new CarryBag();
cBag.a[0] = cBag.new Struct(5.0, 4.0);
cBag.a[1] = cBag.new Struct (3.0, 4.0);
cBag.a[2] = cBag.new Struct (2.0, 3.0);
cBag.a[3] = cBag.new Struct (1.0, 1.0);
cBag.limitW = 7.0;
cBag.maxv = 0.0;
cBag.totV = 12.0;
cBag.n = 4;
for (k = 0; k < cBag.n ; k++) {
cBag.cop[k] = 0;
}
cBag.find(0, 0.0, cBag.totV);
for(k = 0; k < cBag.n ; k++){
if(cBag.option[k] != 0) {
System.out.println(k);
}
}
System.out.println(cBag.maxv);
}
class Struct{
double weight;
double value;
Struct (double weight, double value){
this.weight = weight;
this.value = value;
}
}
}
以上程式碼複製、貼上即可執行!
相關文章
- 揹包問題的遞迴與非遞迴演算法遞迴演算法
- 這樣的執行問題請教高手
- 揹包問題的演算法演算法
- 從【零錢兌換】問題看01揹包和完全揹包問題
- 請看程式碼(關於JAVA的遞迴呼叫)Java遞迴
- 揹包問題(01揹包與完全揹包)
- ofbiz的幾個問題,希望用過的高手給點指點
- JavaScript中揹包問題(面試題)JavaScript面試題
- Java實現-揹包問題IJava
- Java實現-揹包問題IIJava
- Java實現-揹包問題VIJava
- 揹包問題
- JiveJdon 除錯執行了,但有兩個問題除錯
- 求助:Apache的問題請高手指點(轉)Apache
- 二維費用揹包問題
- 【演算法】0-1揹包問題演算法
- 原:八皇后問題的遞迴和非遞迴Java實現遞迴Java
- java學習中遇到的問題 請高手幫忙 線上等Java
- 01揹包問題的解決
- ACM 揹包問題ACM
- 01揹包問題
- 遞迴演算法程式設計整數因子分解問題的遞迴演算法遞迴演算法程式設計
- 30、java中遞迴演算法Java遞迴演算法
- 請教一個切片遞迴賦值的問題遞迴賦值
- acm演算法之三大揹包問題ACM演算法
- Java遞迴演算法的使用Java遞迴演算法
- swing程式打成jar包,執行出現(could not find the main class)問題,請高手指點!!JARAI
- jbuilder9.0+jdk1.5的問題,請高手指點UIJDK
- 揹包問題的一道經典問題
- 提一個巨難的問題,請高手們來討論..
- MySql 漢字問題? 請高手指點!MySql
- 新手求助,junit問題,請高手指點
- 揹包問題大合集
- 關於資料庫中儲存中文的問題,請高手指點資料庫
- 關於Java communications API的問題,請教高手JavaAPI
- 樹遞迴問題的求解遞迴
- 假如我們把函式都改成遞迴...函式遞迴
- C#中漢諾塔問題的遞迴解法C#遞迴