package OJ;
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
w[i] = Integer.parseInt(wei[i]);
p[i] = Integer.parseInt(pri[i]);
}
//将各物品的重量从小到大的排序(选择排序),对应的价值也要排序
for(int i=0;i
int index = i;
int temp_w = w[i];
int temp_p = p[i];
for(int j=i+1;j
if(w[j]
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
for(int i=0;i
if(sum
re[i] = sum/w[i];
sum = sum%w[i];
}
int sum_price = 0;
//求出背包内物品的总价值
for(int i=0;i
sum_price = sum_price+p[i]*re[i];
}
sop(sum_price);
}
public static void sop(Object o){
System.out.print(o);
}
}