frmPak.frm
资源名称:lx.rar [点击查看]
上传用户:sdxhx123
上传日期:2022-08-06
资源大小:3453k
文件大小:5k
源码类别:
数值算法/人工智能
开发平台:
Visual Basic
- VERSION 5.00
- Begin VB.Form frmPak
- Caption = "回溯法求解背包问题"
- ClientHeight = 8220
- ClientLeft = 60
- ClientTop = 465
- ClientWidth = 10005
- Icon = "frmPak.frx":0000
- LinkTopic = "Form1"
- Picture = "frmPak.frx":030A
- ScaleHeight = 8220
- ScaleWidth = 10005
- StartUpPosition = 3 '窗口缺省
- Begin VB.TextBox Text1
- BackColor = &H00C0FFFF&
- BeginProperty Font
- Name = "隶书"
- Size = 15
- Charset = 134
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 4335
- Left = 600
- MultiLine = -1 'True
- TabIndex = 2
- Top = 960
- Width = 4335
- End
- Begin VB.CommandButton Command2
- Caption = "退出"
- BeginProperty Font
- Name = "隶书"
- Size = 15
- Charset = 134
- Weight = 700
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 495
- Left = 6480
- TabIndex = 1
- Top = 6600
- Width = 1815
- End
- Begin VB.CommandButton Command1
- Caption = "录入数据"
- BeginProperty Font
- Name = "隶书"
- Size = 15
- Charset = 134
- Weight = 700
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 495
- Left = 1680
- TabIndex = 0
- Top = 6600
- Width = 1815
- End
- Begin VB.Line Line1
- X1 = 5520
- X2 = 5520
- Y1 = 600
- Y2 = 5760
- End
- Begin VB.Label Label1
- BackColor = &H00C0FFFF&
- BackStyle = 0 'Transparent
- Caption = $"frmPak.frx":D8EA
- BeginProperty Font
- Name = "楷体_GB2312"
- Size = 14.25
- Charset = 134
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- ForeColor = &H00C0FFFF&
- Height = 3495
- Left = 6000
- TabIndex = 3
- Top = 1080
- Width = 3495
- End
- End
- Attribute VB_Name = "frmPak"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- Private Sub Command1_Click()
- Dim i, j, k, Num As Integer
- Dim MaxWeight, WR As Integer '定义变量MaxWeight为最大装载量,W为剩余容量
- Dim W(), P() As Integer '定义每箱的重量和价值
- Dim F() As Integer '最优值函数
- Dim X() As Integer '表示那些物品被放入。放入的是1,否则为零
- Num = Val(InputBox("请输入物品数量:")) '输入箱数和MaxWeight
- Text1 = "物品个数为" & Num & ";" & vbCrLf
- MaxWeight = Val(InputBox("背包容量为:"))
- Text1 = Text1 & "背包的容量为" & MaxWeight & ";" & vbCrLf
- ReDim W(1 To Num) '重新定义动态数组大小
- ReDim P(1 To Num)
- ReDim F(0 To Num, 0 To MaxWeight)
- ReDim X(1 To Num)
- '输入每个物品的重量和价值
- For j = 1 To Num
- W(j) = Val(InputBox("第" & j & "个物品的重量为:"))
- P(j) = Val(InputBox("第" & j & "个物品的价值为:"))
- Text1 = Text1 & "第" & j & "个物品的重量为" & W(j) & ",价值为" & P(j) & ";" & vbCrLf
- Next j
- For i = 1 To Num
- F(i, 0) = 0 '清零
- For WR = 1 To MaxWeight
- If W(i) <= WR Then
- If P(i) + F(i - 1, WR - W(i)) > F((i - 1), WR) Then
- F(i, WR) = P(i) + F(i - 1, WR - W(i))
- Else
- F(i, WR) = F(i - 1, WR)
- End If
- Else
- F(i, WR) = F(i - 1, WR)
- End If
- Next WR
- Next i
- Text1 = Text1 & "最大价值为" & F(Num, MaxWeight) & ";" & vbCrLf
- '从后往上找最优解
- For m = Num To 2 Step -1
- If F(m, MaxWeight) = F(m - 1, MaxWeight) Then
- X(m) = 0
- Else
- X(m) = 1
- MaxWeight = MaxWeight - W(m)
- End If
- Next m
- '对X(1)单独讨论
- If F(1, MaxWeight) = 0 Then
- X(1) = 0
- Else
- X(1) = 1
- End If
- '输出物品序号
- For i = 1 To Num
- temp = temp + X(i)
- Next i
- If temp = 0 Then
- Text1 = Text1 & "没有物品可以放入背包。"
- Else
- For i = 1 To Num
- If X(i) <> 0 Then Text1 = Text1 & i & " "
- Next i
- Text1 = Text1 & "号物品被放入背包。"
- End If
- End Sub
- '退出程序
- Private Sub Command2_Click()
- Unload Me
- End Sub