gpt4 book ai didi

java - 礼品包装算法无法正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:04:16 25 4
gpt4 key购买 nike

我正在尝试在 VB6 中用 Java 实现这个礼品包装算法(yoshihitoyagi!)。我很确定我已经正确地做到了这一点,但由于某种原因,它不会起作用。返回的数组只有 1 个元素。我希望有人能看一看(一双新眼睛)并让我知道我是否公然遗漏了什么。

这是我的代码:

Function small(ByVal Current As Integer, ByVal smallest As Integer, ByVal i As Integer) As Boolean
Dim xa, ya, xb, yb, val As Integer

xa = xPoints(smallest) - xPoints(Current)
xb = xPoints(i) - xPoints(Current)
ya = yPoints(smallest) - yPoints(Current)
yb = yPoints(i) - yPoints(Current)

val = xa * yb - xb * ya

If val > 0 Then
small = True
ElseIf val < 0 Then
small = False
Else
If (xa * xb + ya * yb) < 0 Then
small = False
Else
If (xa * xa + ya * ya) > (xb * xb + yb * yb) Then
small = True
Else
small = False
End If
End If
End If

End Function

Sub CreateContours1()
Dim Min, i, num, smallest, Current, contourcount2 As Integer
Dim xPoints2(), yPoints2() As Long

'Find leftmost lowest point
Min = 1
For i = 1 To contourCount
If yPoints(i) = yPoints(Min) Then
If xPoints(i) < xPoints(Min) Then
Min = i
End If
ElseIf yPoints(i) < yPoints(Min) Then
Min = i
End If
Next

Debug.Print "Min: " & Min
Current = Min
num = 1

Do
contourcount2 = contourcount2 + 1
ReDim Preserve xPoints2(contourcount2)
ReDim Preserve yPoints2(contourcount2)
xPoints2(num) = xPoints(Current)
yPoints2(num) = yPoints(Current)
Debug.Print "num: " & num & ", current: " & Current & "(" & xPoints(Current) & ", " & yPoints(Current) & ")"
num = num + 1
smallest = 1
If smallest = Current Then
smallest = 1
End If

For i = 1 To contourCount
If (Current = i) Or (smallest = i) Then
GoTo continue_loop
End If
If small(Current, smallest, i) Then
smallest = i
End If
Next
Current = smallest
continue_loop:
Loop While Current <> Min

End Sub

我所有的数组都从 1 开始。所以如果您看到 1 和 0 之间有任何差异,这就是原因。

我知道这很多,但我们将不胜感激。

谢谢!!!!!!

最佳答案

这很难说,因为我不知道是否还有其他未显示在类/模块范围内的变量,但您可能使用了一些未声明的变量。

  1. 使用 Option Explicit 并查看是否出现任何编译错误。特别是 contourCount 似乎没有声明。

  2. 您需要显式声明每个变量类型。

这个:

Dim Min, i, num, smallest, Current, contourcount2 As Integer 
Dim xPoints2(), yPoints2() As Long

真的是这样吗:

Dim Min As Variant, i As Variant, num As Variant, smallest As Variant, Current As Variant, contourcount2 As Integer 
Dim xPoints2() As Variant, yPoints2() As Long

所以你应该改成这样:

Dim Min As Long, i As Long, num As Long, smallest As Long, Current As Long, contourcount2 As Long 
Dim xPoints2() As Long, yPoints2() As Long

另请注意,我将它们全部更改为 Long。在 VB6 中几乎没有理由再使用 Integer(2 字节)数据类型。

编辑 1:

All my arrays are starting at 1. So if you see any differences between 1's and 0's that is why.

您是否知道除非您明确声明,否则您的 redim 保留不会保留 1 的下限?所以“ReDim Preserve xPoints2(contourcount2)”分配了一个“zeroeth”槽。如果您想从 1 开始该数组,则可以使用“ReDim Preserve xPoints2(1 to contourcount2)”。

编辑 2:

在你的 Do 循环之外你有 Current = Min

接下来在你的 Do 循环中你有

smallest = 1     
If smallest = Current Then
smallest = 1
End If

这意味着在每个 迭代中最小的是 1。

接下来你有一个总是从 1 开始的 For 循环:

For i = 1 To contourCount         
If (Current = i) Or (smallest = i) Then
GoTo continue_loop
End If
'the rest ommited because you never get here
Next

请注意,小值始终 1,因此您始终会分支。

最后你的分支是这样的:

continue_loop: 
Loop While Current <> Min

当前仍然是 1,只要你的点数在计算 Min 时不在位置 1,那么你将立即满足循环条件并退出。

关于java - 礼品包装算法无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7755559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com