using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test01Bag
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Product
{
#region 商品类
///
/// 商品重量泛型List存储
///
private List
public List
{
get{ return weight; }
set{ weight = value; }
}
private List
///
/// 商品价值泛型List存储
///
public List
{
get { return this.value; }
set{ this.value = value; }
}
private int count;
///
/// 商品数量
///
public int Count
{
get
{
count = weight.Count;
return count;
}
}
///
/// 添加商品信息
///
/// 重量
/// 价值
///
public int setWeightAddValve(int w, int v)
{
weight.Add(w);
value.Add(v);
return weight.Count;
}
#endregion
}
public class Bag
{
#region 背包类
int[,] help = new int[100, 100];
int[] weight = new int[100];
int[] value = new int[100];
int count;
int temp;
// 背包容量
private int valume;
public int Valume
{
get{ return valume; }
set { valume = value; }
}
private int maxvalue;
public int Maxvalue
{
get { return maxvalue; }
set{ maxvalue = value; }
}
///
/// 设定容量temp暂存
///
///
public void setValume(int v)
{
valume = v;
temp = valume;
}
///
/// 引入数据
///
/// Product
public void setProduct(Product newprd)
{
count = newprd.Count;
weight = newprd.Weight.ToArray();
value = newprd.Value.ToArray();
}
///
/// 买商品
///
///
public int buyproduct()
{
//初始化help表第一行为零
for (int w = 0; w < valume; w++)
{
help[0, w] = 0;
}
for (int i = 1; i <= count; i++)
{
// //初始化help表第一l列为零
help[i, 0] = 0;
for (int w = 1; w <= valume; w++)
{
int temp = w - weight[i - 1];
if (weight[i - 1] <= w)
{
if (value[i - 1] + help[i - 1, temp] > help[i - 1, w])
{
help[i, w] = value[i - 1] + help[i - 1, temp];
}
else
{
help[i, w] = help[i - 1, w];
}
}
else
{
help[i, w] = help[i - 1, w];
}
}
}
maxvalue = help[count, valume];
return maxvalue;
}
///
/// 显示结果买的商品状态存入ArrayList
///
///
public ArrayList showResult()
{
ArrayList result = new ArrayList();
if (weight[0] == help[1, temp])
{
result.Add("Buy!");
}
else
{
result.Add("Not Buy!");
}
if (count >= 2)
{
for (int i = count; i <= 2; i--)
{
if (help[i, temp] == help[i - 1, temp])
{
result.Add("Not Buy!");
}
else
{
result.Add("Buy!");
}
temp = temp - weight[i - 1];
}
}
return result;
}
#endregion
}
///
/// 实例化
///
Product newproduct = new Product();
Bag newbag = new Bag();
int i=1;
private void Form1_Load(object sender, EventArgs e)
{
///初始化
onit();
}
public void onit()
{
#region 程序初始化
//清空
newproduct.Value.Clear();
newproduct.Weight.Clear();
newbag.Valume = 0;
lstboxResult.Items.Clear();
#endregion
}
///
/// 存储商品信息函数
///
///
///
private void btnAddProduct_Click(object sender, EventArgs e)
{
#region 添加商品信息
try
{
int w = Convert.ToInt32(txtWeight.Text);
int v = Convert.ToInt32(txtValue.Text);
newproduct.setWeightAddValve(w, v);
i++;
label1.Text = "请输入商品(" + i.ToString() + ")的体积:";
label3.Text = "请输入商品(" + i.ToString() + ")的价值:";
txtWeight.Text = "";
txtValue.Text = "";
}
catch
{
MessageBox.Show("你的输入有错误,请重新输入!");
return;
}
#endregion
}
///
/// 问题解决函数
///
///
///
private void btnToBuy_Click(object sender, EventArgs e)
{
#region 解决背包问题
try
{
int i = 1;
int v = Convert.ToInt32(txtValume.Text);
newbag.setValume(v);
txtValume.Text = "";
newbag.setProduct(newproduct);
int x = newbag.buyproduct();
lblMaxvalue.Text = "最大价值为:" + newbag.buyproduct().ToString();
ArrayList result = newbag.showResult();
int f = result.Count;
foreach (string str in result)
{
string message = "商品" + i.ToString() + "--------" + str;
lstboxResult.Items.Add(message);
i++;
}
onit();
}
catch
{
MessageBox.Show("你的输入有错误,请重新输入!");
return;
}
#endregion
}
}
}
希望把题目写的更详细一点?