/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package joseph;
import java.util.*;
public class KnapSack2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter the number of objects(请输入物品的数量):");
int n = in.nextInt();
int[] w = new int[n];
int[] v = new int[n];
System.out.println("Now,please enter the weight of these objects(现在请输入这些物品的重量):");
for (int i = 0; i < n; i++) {
w[i] = in.nextInt();
}
System.out.println("Now,please enter the value of these objects(现在请输入这些物品的价值):");
for (int i = 0; i < n; i++) {
v[i] = in.nextInt();
}
System.out.println("Now,please enter the capacity of the pack(现在请输入背包的容量):");
int c = in.nextInt();
/**
*按单位重量价值r[i]=v[i]/w[i]降序排列
*/
double startTime = System.currentTimeMillis();
double[] r = new double[n];
int[] index = new int[n];
for (int i = 0; i < n; i++) {
r[i] = (double) v[i] / (double) w[i];
index[i] = i;
}
double temp = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (r[i] < r[j]) {
temp = r[i];
r[i] = r[j];
r[j] = temp;
int x = index[i];
index[i] = index[j];
index[j] = x;
}
}
}
/**
*排序后的重量和价值分别存到w1[]和v1[]中
*/
int[] w1 = new int[n];
int[] v1 = new int[n];
for (int i = 0; i < n; i++) {
w1[i] = w[index[i]];
v1[i] = v[index[i]];
}
System.out.println(Arrays.toString(w1));
System.out.println(Arrays.toString(v1));
/**
*调用函数KnapSackBackTrack(),输出打印装完物品以后的最大价值
*/
KnapSackBackTrack(w1, v1, w1.length, c);
double endTime = System.currentTimeMillis();
System.out.println("Basic Statements take(基本语句用时) " + (endTime - startTime) + " milliseconds!");
}
/**
*用回溯法求0、1背包最大价值的函数定义
*/
public static void KnapSackBackTrack(int[] w, int[] v, int n, int c) {
int CurrentWeight = 0;
int CurrentValue = 0;
int maxValue = 0;
int i = 0;
while (i >= 0) {
if (CurrentWeight + w[i] <= c) {
CurrentWeight += w[i];
CurrentValue += v[i];
i++;
} else {
break;
}
}
if (i < n) {
maxValue = CurrentValue;
System.out.println("Now,the largest values of objects in the pack is(背包中物品的最大价值为):" + maxValue);
return;
}
System.out.println("Now,the largest values of objects in the pack is(背包中物品的最大价值为):" + maxValue);
return;
}
}
java算法 0-1 背包问题回溯算法解决 netbeans
5星 · 超过95%的资源 需积分: 11 33 浏览量
2010-06-11
22:46:29
上传
评论
收藏 6KB RAR 举报
GUThank
- 粉丝: 11
- 资源: 6
最新资源
- VSc++编程助手1.0 - 1
- 基于matlab的说话人语音识别源码+PPT+报告(优质项目).zip
- 语音识别基于matlab说话人识别系统源码+报告PPT(高分优质项目).zip
- 毕业设计: 校园失物招领小程序的设计与实现论文(源码 + 数据库 + 说明文档)
- 美易ME-DUI自绘模块3.3-易语言模块
- 毕业设计:小程序消防知识每天学(源码 + 数据库 + 说明文档)
- 2024-NEW.zip
- 软件.zip
- 华为FPGA设计技巧FPGA设计规范设计经验设计流程指南华为面经硬件工程师手册等资料(15个).zip
- 毕业设计:基于微信小程序大学校园二手教材与书籍拍卖系统(源码 + 数据库 + 说明文档)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页