背包问题的贪心算法
来源:互联网 发布:龙华新区行知实验小学 编辑:程序博客网 时间:2024/04/29 19:38
package OJ;
import java.util.*;
public class Bully {
/*
* 背包贪心算法
* 问题描述:
* 一个旅行者有一个最多能用m公斤的背包,现在有n种物品,每件的重量分别是W1,W2,...,Wn,
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//输入背包可以装的总重量
int sum = Integer.parseInt(sc.nextLine());
//输入物品数量n
int n = Integer.parseInt(sc.nextLine());
//输入重量和价值
String weight = sc.nextLine();
String price = sc.nextLine();
String[] wei = weight.split(" ");
String[] pri = price.split(" ");
int[] w = new int[n];
int[] p = new int[n];
for(int i=0;i<n;i++){
w[i] = Integer.parseInt(wei[i]);
p[i] = Integer.parseInt(pri[i]);
}
//将各物品的重量从小到大的排序(选择排序),对应的价值也要排序
for(int i=0;i<n;i++){
int index = i;
int temp_w = w[i];
int temp_p = p[i];
for(int j=i+1;j<n;j++){
if(w[j]<temp_w){
index = j;
temp_w = w[j];
temp_p = p[j];
}
}
w[index] = w[i];
p[index] = p[i];
w[i] = temp_w;
p[i] = temp_p;
}
//计算出可以放进背包的各物品的数量,放进re数组中
int[] re = new int[n];
for(int i=0;i<n;i++)
re[i] = 0;
for(int i=0;i<n;i++){
if(sum<w[i])
break;
re[i] = sum/w[i];
sum = sum%w[i];
}
int sum_price = 0;
//求出背包内物品的总价值
for(int i=0;i<n;i++){
sum_price = sum_price+p[i]*re[i];
}
sop(sum_price);
}
public static void sop(Object o){
System.out.print(o);
}
}
import java.util.*;
public class Bully {
/*
* 背包贪心算法
* 问题描述:
* 一个旅行者有一个最多能用m公斤的背包,现在有n种物品,每件的重量分别是W1,W2,...,Wn,
* 每件的价值分别为C1,C2,...,Cn.若的每种物品的件数足够多.求旅行者能获得的最大总价值。
* */public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//输入背包可以装的总重量
int sum = Integer.parseInt(sc.nextLine());
//输入物品数量n
int n = Integer.parseInt(sc.nextLine());
//输入重量和价值
String weight = sc.nextLine();
String price = sc.nextLine();
String[] wei = weight.split(" ");
String[] pri = price.split(" ");
int[] w = new int[n];
int[] p = new int[n];
for(int i=0;i<n;i++){
w[i] = Integer.parseInt(wei[i]);
p[i] = Integer.parseInt(pri[i]);
}
//将各物品的重量从小到大的排序(选择排序),对应的价值也要排序
for(int i=0;i<n;i++){
int index = i;
int temp_w = w[i];
int temp_p = p[i];
for(int j=i+1;j<n;j++){
if(w[j]<temp_w){
index = j;
temp_w = w[j];
temp_p = p[j];
}
}
w[index] = w[i];
p[index] = p[i];
w[i] = temp_w;
p[i] = temp_p;
}
//计算出可以放进背包的各物品的数量,放进re数组中
int[] re = new int[n];
for(int i=0;i<n;i++)
re[i] = 0;
for(int i=0;i<n;i++){
if(sum<w[i])
break;
re[i] = sum/w[i];
sum = sum%w[i];
}
int sum_price = 0;
//求出背包内物品的总价值
for(int i=0;i<n;i++){
sum_price = sum_price+p[i]*re[i];
}
sop(sum_price);
}
public static void sop(Object o){
System.out.print(o);
}
}
0 0
- 背包问题的贪心算法
- 背包问题的贪心算法
- 贪心算法 - 背包问题
- 贪心算法----背包问题
- 【贪心算法】:背包问题
- 贪心算法-背包问题
- 贪心算法 背包问题
- 贪心算法-背包问题
- 【贪心算法】背包问题
- 贪心算法-背包问题
- 贪心算法--背包问题
- 背包问题-贪心算法
- 用贪心算法实现的背包问题
- 0/1背包问题的贪心算法
- 背包问题的贪心算法求解
- 基于贪心算法的背包问题
- 一般背包问题的贪心算法
- 贪心算法在背包背包问题中应用的探讨
- CentOS7 下安装 mysql
- MFC_CString TCHAR的互相转换
- mac命令行打开sublime
- Nhibernate报错:Initializing[Sys_User#1]-Could not initialize proxy - no Session.
- java 操作mongodb查询条件的常用设置
- 背包问题的贪心算法
- 原型模式
- 正则表达式表示输入的内容是否为数字、字母、下划线、汉字
- 传参
- Mac 常用快捷键
- PHPExcel开发者文档[中文版]
- 【Axure学习】(二)Axure遮罩层
- Android LBS地图开发:地球地理GPS坐标系经纬度偏移偏差
- python 读写Json的中文编码问题