首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VB.net -9项的排列(车轮的平衡辐条)

VB.net -9项的排列(车轮的平衡辐条)
EN

Stack Overflow用户
提问于 2016-06-04 02:17:49
回答 1查看 109关注 0票数 0

我有一个车轮,需要9个辐条,由于制造公差,每个轮辐有一个不同的重量。

我需要安排轮上的辐条,以便它必须至少失去平衡。

计算向量(复system.numerics)的平衡I和,即

1= Complex.FromPolarCoordinates(weight#1,0)

2= Complex.FromPolarCoordinates(weight#2,2*math.pi/9)

在执行了所有的计算之后,我得到了结果,并保存了复杂的实数,Complex.real

然后我改变辐条的顺序,重新计算complex.real。

我有两个问题,

1)如何计算排列,有效地改变顺序?我想避免(362880) 9的9个嵌套循环!排列?

( 2)进行迭代有捷径吗?

我不确定还有哪些其他的置换应用程序可以用作比较。

我最关心的是效率问题,我今天勾画了代码,并被困在了置换部分。稍后我会发布一些代码。

提前感谢

我创建了一个辐条和重量类,由此我可以测试许可。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-06 16:19:54

下面是我的代码,它是基于Donald开发的一个经典算法。(顺便说一句,他写了一系列很棒的书。)您可能可以将字节更改为整数,因为只有9!= 362880排列。若要使用,请创建一个值为0- 8的字节列表。(按此顺序!)这是你的第一次排列。在Do循环中,使用列表调用算法,直到返回false为止。每次调用该算法时,该列表将被重新排列到下一个置换。

代码语言:javascript
复制
Public Function NextPermutation(numList As List(Of Byte)) As Boolean
    '   Donald Knuth's algorithm from the "Art of Computer Programming"
    '   1. Find the largest index j such that a[j] < a[j + 1]. If no such index exists, the permutation is the last permutation.
    '   2. Find the largest index l such that a[j] < a[l]. Since j + 1 is such an index, l is well defined and satisfies j < l.
    '   3. Swap a[j] with a[l].
    '   4. Reverse the sequence from a[j + 1] up to and including the final element a[n].
    '   To get all the permutations, one must start with the 'first' one, which is defined as having all items in ascending order, for example 12345.
    Dim largestIndex As Integer = -1
    Dim i, j As Integer
    For i = numList.Count - 2 To 0 Step -1
        If numList(i) < numList(i + 1) Then
            largestIndex = i
            Exit For
        End If
    Next
    If largestIndex < 0 Then Return False
    Dim largestIndex2 As Integer = -1
    For i = numList.Count - 1 To 0 Step -1
        If numList(largestIndex) < numList(i) Then
            largestIndex2 = i
            Exit For
        End If
    Next
    Dim tmp As Byte = numList(largestIndex)
    numList(largestIndex) = numList(largestIndex2)
    numList(largestIndex2) = tmp
    i = largestIndex + 1
    j = numList.Count - 1
    While i < j
        tmp = numList(i)
        numList(i) = numList(j)
        numList(j) = tmp
        i += 1
        j -= 1
    End While

    Return True
End Function

在开始排列之前,还应该预先计算出所能做的事情。例如,将2*math.pi/9保存到局部变量并使用该变量。也许您也可以避免重复调用Complex.FromPolarCoordinates,但我没有深入了解您的算法的细节,所以我不确定是否可以降低。

下面是一个关于如何使用该函数的快速示例:

代码语言:javascript
复制
    Dim spokes As New List(Of Byte)
    For i As Byte = 0 To 8
        spokes.Add(i)
    Next

    Do
        'Do you balance calculation here.


    Loop While NextPermutation(Spokes)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37625701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档