A fun math problem

zhaozj2021-02-16  92

topic:

Between , -, *, / added to between 1, 2, 3, 4, 5, 6, 7, 8, 9, to make it composed of the calculation results equal to 100, such as: 1 * 2 * 3 * 4 5 6 7 * 8 9 = 100

analysis:

The above equivalents are equivalent to a special order of 1 to 99 numbers and 8 ( , -, *, /), with a total of 4 ^ 8 = 65536 possible, and then hindly.

Code:

Option EXPLICITELINE LIB "VBA6.DLL" (Byval PstringtoExec As Long, Byval unknownn2 as long, Byval Fcheckonly As long) AS Long 'API

Private function executeline (Scode As String, Optional Fcheckonly As Boolean) AS BooleanexecuteLine = EbexecuteLine (Strptr (Scode), 0 &, 0 &, Abs (Fcheckonly) = 0nd Function

Private Function Result (BYVAL X AS STRING) AS SINGLE 'Computational Expression Results EXECUTELINE "DIM X as Single" ExecuteLine "X =" & XexecuteLine "Clipboard.SetText X" "Send to Cutting Board Result = Clipboard.getText' Clipboard.clear 'Clear Shearing Board End Function

Sub main () 'start calculating getAll 100nd Sub

Private Sub getAll (Byval theResult As INTEGER) DIM TEMP AS long 'four-input intermediate variable DIM X (8) AS string' Get 8 positions four operator DIM OP (3) AS String 'Defines four four operators DIM I as long, J AS Integer 'Cycle Variable DIM OUT AS STRING' Final Expression DIM All As Long, Num as Integer 'Cycle Range and Output Count Num = 0Op (0) = " " OP (1) = "-" Op (2) = "*" op (3) = "/"

ALL = 4 ^ 8 - 1

For i = 0 to allTemp = ix (0) = "1.0" for j = 1 to 8X (j) = op (TEMP MOD 4) & format (j 1, "0.0") TEMP = TEMP / 4NEXTOUT = JOIN x, "") = theresult the 'condition judgment NUM = Num 1debug.print "Solution" & Num & ":" & VbTab & Replace (OUT, ".0", "") & "= "& areesultend ifxtif num = 0 Then Debug.print" No solution! "if Num> 0 Then Debug.print" "& Num &" Follow! "End Sub Output:

Solution 1: 1 * 2 * 3 * 4 5 6 7 * 8 9 = 100 solution 2: 1-2 3 * 4 * 5 6 * 7 8-9 = 100 solutions 3: 1-2 3 * 4 * 5-6 7 * 8-9 = 100 solutions 4: 1 2 3 4 5 6 7 8 * 9 = 100 solution 5: 1 * 2 * 3 4 5 6 7 8 * 9 = 100 solutions 6: 1-2 * 3 4 * 5 6 7 8 * 9 = 100 solutions 7: 1 2 * 3 4 * 5-6 7 8 * 9 = 100 solutions 8: 1-2 * 3-4 5 * 6 7 8 * 9 = 100 solutions 9: 1 2-3 * 4 5 * 6 7 8 * 9 = 100 solutions 10 : 1 2 * 3 * 4 * 5/6 7 8 * 9 = 100 solutions 11: 1 * 2 * 3 * 4 5 6-7 8 * 9 = 100 solutions 12: 1-2 * 3 -4-5 6 * 7 8 * 9 = 100 solutions 13: 1 2-3 * 4-5 6 * 7 8 * 9 = 100 solutions 14: 1 2 3-4 * 5 6 * 7 8 * 9 = 100 solutions 15: 1 * 2 * 3-4 * 5 6 * 7 8 * 9 = 100 total 15 sets!

转载请注明原文地址:https://www.9cbs.com/read-13468.html

New Post(0)