贪心法 求解背包问题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验三:贪心法求解背包问题
一、实验目的与要求
1、掌握背包问题的算法
2、初步掌握贪心算法
二、实验题
问题描述:与0-1背包问题相似,给定n种物品和一个背包。物品i的重量是wi,其价值为vi,背包的容量为c。与0-1背包问题不同的是,在选择物品i装入背包时,背包问题的解决可以选择物品i的一部分,而不一定要全部装入背包,1< i < n。
三、实验代码
package xp3;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Greedy extends JFrame {
private static final long serialVersionUID= -1508220487443708466L;
private static final int width = 360;// 面板的宽度
private static final int height = 300;// 面板的高度
public int M;
public int[] w;
public int[] p;
public int length;
Greedy() {
// 初始Frame参数设置
this.setTitle("贪心算法");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(width, height);
Container c = getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
setLocation(350, 150);
// 声明一些字体样式
Font topF1 = new Font("宋体", Font.BOLD, 28);
Font black15 = new Font("宋体", Font.PLAIN, 20);
Font bold10 = new Font("宋体", Font.BOLD, 15);
// 声明工具栏及属性设置
JPanel barPanel = new JPanel();
JMenuBar topBar = new JMenuBar();
topBar.setLocation(1, 1);
barPanel.add(topBar);
// 面板1和顶部标签属性设置
JPanel p1 = new JPanel();
JLabel topLabel = new JLabel("背包问题");
topLabel.setForeground(Color.blue);
topLabel.setFont(topF1);
p1.add(topLabel);
// 中间面板和标签及输入框属性设置
JPanel p2 = new JPanel();
p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));
JLabel wLabel = new JLabel("重量:");
JLabel pLabel = new JLabel("效益:");
wLabel.setFont(black15);
pLabel.setFont(black15);
//
final JTextField wText = new JTextField(8);
final JTextField pText = new JTextField(8);
p2.add(wLabel);
p2.add(wText);
p2.add(pLabel);
p2.add(pText);
//
// 中下部面板和标签属性设置
JPanel p3 = new JPanel();
JLabel bottomLabel = new JLabel("注意:输入多个时请添加分割符‘,’"); bottomLabel.setFont(bold10);
bottomLabel.setForeground(Color.red);
bottomLabel.setHorizontalAlignment(SwingConstants.RIGHT);
p3.add(bottomLabel);
JPanel p5 = new JPanel();
p5.setLayout(new BoxLayout(p5, BoxLayout.Y_AXIS));
JLabel mLabel = new JLabel(" 背包总重量:");
mLabel.setFont(black15);
final JTextField mText = new JTextField(8);
p5.add(mLabel);
p5.add(mText);
// 面板和按钮的设置
JPanel p4 = new JPanel();
JButton submit = new JButton("确定");
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1 = pText.getText(); //效益
String s2 = wText.getText(); //重量
String s3 = mText.getText(); //背包
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
String s1Copy,s2Copy,s3Copy;
s1Copy = s1;
s2Copy = s2;
s3Copy = s3;
s1Copy = s1Copy.replaceAll(",","");
s2Copy = s2Copy.replaceAll(",","");
s3Copy = s3Copy.replaceAll(",","");
System.out.println(s1Copy);
System.out.println(s2Copy);
System.out.println(s3Copy);
try{
String[] temp = s1.split(",");// 按照‘,’分割字符串
p = new int[temp.length];
for (int i = 0; i < temp.length; i++)
p[i] = Integer.parseInt(temp[i]);//
temp = s2.split(",");
w = new int[temp.length];
for (int i = 0; i < temp.length; i++) w[i] =
Integer.parseInt(temp[i]);//
if (w.length == p.length)
{
length = w.length;
}
else {
JOptionPane.showMessageDialog(null, "长度不等,检查后重新输入!"); }