1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| 'Sources: https://codes-sources.commentcamarche.net/source/22392-le-compte-est-bon
Option Explicit
Private Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
Private Declare PtrSafe Function GetFrequency Lib "kernel32" Alias "QueryPerformanceFrequency" (cyFrequency As Currency) As Long
Private Const Operateur As String = "+*/-"
Private PlusProche As Integer
Private Ecart As Integer
Private Compteur As Long
'---------------------------------------------------------------------------------------
Sub main()
'---------------------------------------------------------------------------------------
Dim Plaque(0 To 5)
Dim Nombre As Integer
Dim ATrouver As Integer
Dim iSec As Long, iFreq As Currency
Plaque(0) = 5
Plaque(1) = 10
Plaque(2) = 3
Plaque(3) = 50
Plaque(4) = 4
Plaque(5) = 9
Nombre = 743
Ecart = Nombre
ATrouver = Nombre
iSec = GetTickCount()
GetFrequency iFreq
Compteur = 0
If Compte(Plaque, 6, Nombre) = True Then
iSec = GetTickCount() - iSec
Debug.Print "Le compte est bon ! en " & Compteur & " tests en " & Format(iSec / iFreq, "##0.000") & " seconde(s)"
Else
Compteur = -1000
Call Compte(Plaque, 6, PlusProche)
Debug.Print "Meilleure solution: (écart " & Abs(PlusProche - ATrouver) & ")"
End If
Debug.Print "------------------------------------------------"
End Sub
'---------------------------------------------------------------------------------------
Function Compte(Plaque As Variant, Nombre As Integer, Total As Integer) As Boolean
'---------------------------------------------------------------------------------------
Dim i, j, k, T(5), l
Compteur = Compteur + 1
If Compteur > 999999 Then Exit Function
For i = 0 To Nombre - 2
For j = i + 1 To Nombre - 1
For k = 1 To 4
For l = 0 To 5: T(l) = Plaque(l): Next l
Select Case k
Case 1:
T(i) = T(i) + T(j)
If T(i) = Total Then
Debug.Print Plaque(i), Mid(Operateur, k, 1), Plaque(j), "=", T(i)
Compte = True: Exit Function
End If
If Abs(T(i) - Total) < Ecart Then PlusProche = T(i): Ecart = Abs(T(i) - Total)
If Nombre > 0 Then T(j) = T(Nombre - 1)
If (Compte(T, Nombre - 1, Total)) = True Then
Debug.Print Plaque(i), Mid(Operateur, k, 1), Plaque(j), "==", T(i)
Compte = True: Exit Function
End If
Case 2:
T(i) = T(i) * T(j)
If T(i) = Total Then
Debug.Print Plaque(i), Mid(Operateur, k, 1), Plaque(j), "=", T(i)
Compte = True: Exit Function
End If
If Abs(T(i) - Total) < Ecart Then PlusProche = T(i): Ecart = Abs(T(i) - Total)
If Nombre > 0 Then T(j) = T(Nombre - 1)
If (Compte(T, Nombre - 1, Total)) = True Then
Debug.Print Plaque(i), Mid(Operateur, k, 1), Plaque(j), "==", T(i)
Compte = True: Exit Function
End If
Case 3:
If T(i) > T(j) And T(i) > 0 And T(j) > 0 Then
If CDec(T(i) / T(j)) = CDec(T(i) \ T(j)) Then
T(i) = T(i) / T(j)
If T(i) = Total Then
Debug.Print Plaque(i), Mid(Operateur, k, 1), Plaque(j), "=", T(i)
Compte = True: Exit Function
End If
If Abs(T(i) - Total) < Ecart Then PlusProche = T(i): Ecart = Abs(T(i) - Total)
If Nombre > 0 Then T(j) = T(Nombre - 1)
If (Compte(T, Nombre - 1, Total)) = True Then
Debug.Print Plaque(i), Mid(Operateur, k, 1), Plaque(j), "==", T(i)
Compte = True: Exit Function
End If
End If
End If
Case 4:
If T(i) < T(j) Then
T(i) = T(j) - T(i)
Else
T(i) = T(i) - T(j)
End If
If T(i) = Total Then
If Plaque(i) > Plaque(j) Then
Debug.Print Plaque(i), Mid(Operateur, k, 1), Plaque(j), "=", T(i)
Else
Debug.Print Plaque(j), Mid(Operateur, k, 1), Plaque(i), "=", T(i)
End If
Compte = True: Exit Function
End If
If Abs(T(i) - Total) < Ecart Then PlusProche = T(i): Ecart = Abs(T(i) - Total)
If Nombre > 0 Then T(j) = T(Nombre - 1)
If (Compte(T, Nombre - 1, Total)) = True Then
If Plaque(i) > Plaque(j) Then
Debug.Print Plaque(i), Mid(Operateur, k, 1), Plaque(j), "==", T(i)
Else
Debug.Print Plaque(j), Mid(Operateur, k, 1), Plaque(i), "==", T(i)
End If
Compte = True: Exit Function
End If
End Select
Next k
Next j
Next i
End Function |
Partager