充电自动矩阵算法

时间:2018-03-19 21:43:55

标签: vba algorithm factorial

我需要算法来为2^k列和2^k行的矩阵充电,其符号值为-1+1。第一个k+1列会向其收取费用,但其余(k值的所有组合)不会收费。 我需要将k exp的所有可能性相乘: 我有k = 3 我需要将1*21*3以及2*31*2*3相乘。 这是一个简单的例子,但我需要一般的解决方案。 此方法用于计算2^k阶乘设计。

我用visual basic尝试了以下代码,但它没有100%工作(只有矩阵的第一(k + 1)列是正确的)

Private Sub Command1_Click()
MSFlexGrid1.Cols = Text1.Text + 1
MSFlexGrid1.Rows = 3
MSFlexGrid1.Col = 0
MSFlexGrid1.Row = 1

MSFlexGrid1.Text = "Min"
MSFlexGrid1.Row = 2
MSFlexGrid1.Text = "Max"
For i = 1 To Text1.Text
MSFlexGrid1.Row = 0
MSFlexGrid1.Col = i
MSFlexGrid1.Text = "F" & Str(i)
Next i
End Sub
 Function FactFunct(ByVal M As Integer) As Integer
    Dim l As Integer
    l = 0
    For j = M - 1 To 1 Step -1
    l = l + j
    Next j
    FactFunct = l
 End Function
Private Sub Command2_Click()
Dim t As Integer

GR2.Visible = True
GR2.Cols = 2 ^ Val(Text1.Text) + 2
GR2.Rows = 2 ^ Val(Text1.Text) + 3
t = 1
GR2.Col = 1



'-------------------------- Incrementer les colonnes

For i = 1 To Val(Text1.Text) + 1
GR2.Row = 1

'-------------------------- declanchement de la routine
If i > 2 Then
t = t * 2
End If
c = -1
j = 1
l = 2 ^ Val(Text1.Text)

'-------------------------- Incrementer les lignes
Do While j <= l

        '-------------------------- chargement de la colonne 1
        If GR2.Col = 1 Then
        GR2.Text = 1
        GR2.Row = GR2.Row + 1
        j = j + 1
        Else

        b = 1

        '-------------------------- Incrementer les colonnes
        Do While b <= t
        GR2.Text = 1 * c

        GR2.Row = GR2.Row + 1


        b = b + 1
        j = j + 1
        Loop


        c = c * -1



        End If



   Loop


GR2.Col = GR2.Col + 1

Next i




'-----------------------------produit des colonnes
GR2.Col = Val(Text1.Text) + 1
Dim n, Puissance, PositionText, Nbrboucle, NbrValPro, ValColFix, PosCol As Integer
GridRout.Cols = Val(Text1.Text) - 1
Dim JJ As Integer
JJ = 0

For p = Val(Text1.Text) To 2 Step -1



GridRout.Col = JJ
GridRout.Text = FactFunct(p)
JJ = JJ + 1
Next p

For nbrrow = 1 To 2 ^ Val(Text1.Text)

routine = 2
charg = Val(Text1.Text) + 2

For i = 1 To 2

For j = 2 To Val(Text1.Text) + 1


res = 1
k = j

Do While i < routine

GR2.Row = nbrrow
GR2.Col = k
res = Val(res) * Val(GR2.Text)

k = k + 1
i = i + 1

If k = Val(Text1.Text) + 1 Then
k = j + 1

End If

Loop




For M = j + 1 To Val(Text1.Text) + 1

GR2.Row = nbrrow
GR2.Col = M

res2 = Val(res) * Val(GR2.Text)

GR2.Col = charg
GR2.Text = Val(res2)

charg = charg + 1

Next M



Next j

routine = routine + 1

Next i

Next nbrrow






End Sub
Private Sub Form_Load()
'GR2.Visible = False
'GridRout.Visible = False

End Sub


Private Sub MSFlexGrid1_DblClick()
MSFlexGrid1.Text = InputBox("give a number")
End Sub

Private Sub MSFlexGrid2_Click()

End Sub

0 个答案:

没有答案