贪心算法java实现代码.docx
《贪心算法java实现代码.docx》由会员分享,可在线阅读,更多相关《贪心算法java实现代码.docx(26页珍藏版)》请在冰豆网上搜索。
![贪心算法java实现代码.docx](https://file1.bdocx.com/fileroot1/2023-3/28/5894611d-e056-42eb-b410-4dd6cbba6241/5894611d-e056-42eb-b410-4dd6cbba62411.gif)
贪心算法java实现代码
贪心算法背包问题java代码
packagetxsf;
importjava.util.*;
publicclassBag{
publicintgetCapacity(){
returncapacity;
}
publicvoidsetCapacity(intcapacity){
this.capacity=capacity;
}
publicfloatgetValue(){
returnvalue;
}
publicvoidsetValue(floatvalue){
this.value=value+this.value;
}
privateintcapacity;//背包容量
privatefloatvalue;//背包内价值
privateRandomrand;
publicBag(){//自动生成背包
rand=newRandom();
capacity=rand.nextInt(150);
if(capacity<50){
capacity+=rand.nextInt(70);
}
System.out.println("背包容量:
"+capacity);
value=0;
}
publicBag(intcapacity){//手工生成背包
this.capacity=capacity;
System.out.println("背包容量:
"+capacity);
}
publicfloatgetvalue(){
returnvalue;
}
}
/*abstractclassComputeItems{
publicListlist=newArrayList();
//public
}*/
classItems{
privateStringimageStr;//物品名称
privateintcapacity;//物品空间
privateintvalue;//物品价值
privateRandomrand;
publicItems(Strings){//自动生成物品
rand=newRandom();
this.imageStr=s;
this.value=rand.nextInt(50)+1;
this.capacity=rand.nextInt(50)+1;
System.out.println(s+":
"+this.capacity+""+this.value);
}
publicItems(Strings,intcapacity,intvalue){//手工生成物品
this.imageStr=s;
this.capacity=capacity;
this.value=value;
}
publicfloatgetCapacity(){
returnthis.capacity;
}
publicfloatgetValue(){
returnthis.value;
}
publicStringgetimageStr(){
returnthis.imageStr;
}
publicStringtoString(){
return""+imageStr+":
容量为:
"+capacity+"价值为:
"+value;
}
}
interfaceCompute{
float[]Sort();
MapgetMap();
}
//重量优先计算
classCapaimplementsCompute{
privateListlist;
privatefloat[]array;//排序物品权值
privateMapmapItems;//哈希表,用来存放物品权值与物品
publicCapa(Listlist){
this.list=list;
mapItems=newHashMap();
array=newfloat[list.size()];
}
publicfloat[]Sort(){
inti=0;
for(Itemsitems:
list){
array[i++]=items.getCapacity();//将物品权重放入数组
mapItems.put(items.getCapacity(),items);//将物品权重与物品对象放入map对象
}
Arrays.sort(array);//物品排序升序
returnarray;
}
@Override
publicMapgetMap(){
//TODOAuto-generatedmethodstub
returnmapItems;
}
publicStringtoString(){
return"重量优先";
}
}
//价值优先计算
classValueimplementsCompute{
privateListlist;
privatefloat[]array;
privateMapmapItems;
publicValue(Listlist){
this.list=list;
mapItems=newHashMap();
array=newfloat[list.size()];
}
publicfloat[]Sort(){
inti=0;
for(Itemsitems:
list){
array[i++]=items.getValue();
mapItems.put(items.getValue(),items);
}
Arrays.sort(array);
returnarray;
}
@Override
publicMapgetMap(){
//TODOAuto-generatedmethodstub
returnmapItems;
}
publicStringtoString(){
return"价值优先";
}
}
//单位价值优先
classPreValueimplementsCompute{
privateListlist;
privatefloat[]array;
privateMapmapItems;
publicPreValue(Listlist){
this.list=list;
mapItems=newHashMap();
array=newfloat[list.size()];
}
publicfloat[]Sort(){
inti=0;
for(Itemsitems:
list){
floatf=items.getValue()/items.getCapacity();
array[i++]=f;
mapItems.put(f,items);
}
Arrays.sort(array);
returnarray;
}
@Override
publicMapgetMap(){
//TODOAuto-generatedmethodstub
returnmapItems;
}
publicStringtoString(){
return"单位价值优先";
}
}
//自定义优先
classCustomFormimplementsCompute{
privateListlist;
privatefloat[]array;
privateMapmapItems;
floatf1,f2;
publicCustomForm(Listlist,floatf1,floatf2){
this.list=list;
this.f1=f1;
this.f2=f2;
mapItems=newHashMap();
array=newfloat[list.size()];
System.out.println(""+list.size());
}
publicfloat[]Sort(){
inti=0;
for(Itemsitems:
list){
floatf=f1*items.getCapacity()+f2*items.getValue();
System.out.println(f);
array[i++]=f;
mapItems.put(f,items);
}
Arrays.sort(array);
returnarray;
}
@Override
publicMapgetMap(){
//TODOAuto-generatedmethodstub
returnmapItems;
}
publicStringtoString(){
return"自定义优先级";
}
}
classShow{
privatestaticfloat[]array;
privatestaticMapmap;//权值与物品对应哈希表
privatestaticMapmapcard;//物品与JPanel对象对应哈希表
privatestaticBagbag;//背包对象
privateintcapacity;
privatefloatvalue;
publicShow(float[]array,Mapmap,Bagbag,Mapmapcard){
this.array=array;
this.map=map;
this.bag=bag;
this.mapcard=mapcard;
capacity=bag.getCapacity();
value=bag.getValue();
}
publicfloatpack(){
System.out.println("装包结果为:
");
for(intj=array.length;j>0;j--){
Floatfx=array[j-1];
Itemsitems=(Items)map.get(fx);//取对应物品权重值对应物品对象
Cardc=(Card)mapcard.get(items);//权重值对应JPanel对象
inti=capacity-(int)items.getCapacity();
if(i>0){
System.out.println("1"+items);
c.Box.setSelected(true);
capacity=(int)(capacity-items.getCapacity());
value=items.getValue()+value;
System.out.println("剩余空间:
"+capacity);
}else{
floatf1=capacity/items.getCapacity()*items.getValue();
value=value+f1;
c.Box.setSelected(true);
System.out.println("2"+items);
System.out.println("剩余空间:
"+capacity);
break;
}
System.out.println("剩余容量为:
"+capacity+"当前价值为:
"+value);
}
System.out.println("剩余容量为:
v"+capacity+"当前价值为:
"+value);
returnvalue;
}
}
packagetxsf;
importjavax.swing.*;
publicclassCardextendsJPanel{
privateJTextFieldCata;
privateJTextFieldValue;
privateJLabelLabel;
publicJCheckBoxBox;
privateStringimage;
privatefloatcatacity,value;
publicCard(Stringimage,floatf,floatg){
setBorder(newTitledBorder(UIManager.getBorder("TitledBorder.border"),image,TitledBorder.LEADING,TitledBorder.TOP,null,null));
setBounds(newRectangle(100,0,100,100));
this.image=image;
this.catacity=f;
this.value=g;
setLayout(null);
Label=newJLabel("Newlabel");
Label.setIcon(newImageIcon("./img/"+image+".png"));
Label.setBounds(1,28,64,64);
add(Label);
Cata=newJTextField();
Cata.setEditable(false);
Cata.setText("\u5BB9\u91CF"+f);
Cata.setBounds(61,0,66,21);
add(Cata);
Cata.setColumns(10);
Value=newJTextField();
Value.setEditable(false);
Value.setForeground(Color.BLACK);
Value.setText("\u4EF7\u503C"+g);
Value.setBounds(61,22,66,21);
add(Value);
Value.setColumns(10);
Box=newJCheckBox("\u88C5\u5165");
Box.setEnabled(false);
Box.setBounds(71,49,103,23);
add(Box);
}
}
packagetxsf;
importjavax.swing.*;
publicclassGreedy_nextextendsJFrame{
privateJTextFieldBagText;
privateJTextFieldCampText;
privateJTextFieldValueText;
privateJTextFieldCusCampText;
privateJTextFieldCumValText;
privateJButtonBagButton;
privateJButtonStartCom;
privateJButtonAddItems;
privateJComboBoxNum;
privateJComboBoxSpe;
privateJPanelpane;
privateString[]items="blessbottlechickencloakgloveHPPotionMPPotionringshieldstarstocktree".split("");
booleanbagboolean=false;//背包生成自动非自动判断
booleanItemboolean=false;//物品自动生成判断
intcompute;//计算使用何种方式排序
privateBagbag;
privateListlist;
privateComputecompSort=null;
publicMapline;
Cardcard;
intcount=0;
publicGreedy_next(){
line=newHashMap();
list=newArrayList();
pane=newJPanel();
classKeyTextimplementsKeyListener{
@Override
publicvoidkeyPressed(KeyEvente){
}
@Override
publicvoidkeyReleased(KeyEvente){}
@Override
publicvoidkeyTyped(KeyEvente){
intkeyChar=e.getKeyChar();
if(keyChar>=KeyEvent.VK_0&&keyChar<=KeyEvent.VK_9){
}else{
e.consume();
}//TODOAuto-generatedmethodstub
}
}
getContentPane().setLayout(null);
JPanelpanel=newJPanel();
panel.setBounds(0,0,324,284);
getContentPane().add(panel);
panel.setLayout(null);
JPanelpanel_3=newJPanel();
panel_3.setBounds(10,5,304,98);
panel.add(panel_3);
panel_3.setLayout(null);
JRadioButtonCustomBag=newJRadioButton("\u624B\u5DE5\u751F\u6210\u80CC\u5305");
CustomBag.setBounds(139,0,121,23);
panel_3.add(CustomBag);
CustomBag.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
BagText.setEnabled(true);
BagText.setEditable(true);
BagButton.setEnabled(true);
bagboolean=true;
}
});
JRadioButtonAutoBag=newJRadioButton("\u81EA\u52A8\u751F\u6210\u80CC\u5305");
AutoBag.setBounds(0,0,121,23);
panel_3.add(AutoBag);
AutoBag.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
BagText.setEnabled(true);
BagButton.setEnabled(true);
bagboolean=false;
}
});
JPanelpanel_5=newJPanel();
panel_5.setBorder(newTitledBorder(null,"\u8BF7\u8F93\u5165\u80CC\u5305\u5BB9\u91CF",TitledBorder.LEADING,TitledBorder.TOP,null,null));
panel_5.setBounds(10,35,140,53);
panel_3.add(panel_5);
panel_5.setLayout(null);
BagText=newJTextField();
BagText.setEditable(false);
BagText.setBounds(0,23,140,30);
panel_5.add(BagText);
BagText.setColumns(10);
BagText.addKeyListener(newKeyText());
//背包按钮事件
BagButton=newJButton("\u751F\u6210\u80CC\u5305");
BagButton.setEnabled(false);
BagButton.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
if(bagboolean){
String