在英國有一個數學遊戲,給參賽者一些正整數和一個目標數,參賽者必須在這些正整數間插入+、-、*或 / 的符號,使得最後計算的結果等於目標數。計算的方式是由左到右,而且不必管運算的優先順序(就是不管先乘除後加減那一套)。
在這個數學運算式中,有三個限制:
1. 正整數出現的次序不可改變,也就是說要與輸入的順序相同。
2. 因為目標數也是一個正整數,所以在運算的過程中,你只有在可以整除的情況下,才可以使用除法。
3. 在運算的過程中,如果你用某一個運算符號,會導致產生的數超出-32000 ~ +32000的範圍,那麼你不可以採用此運算符號(也就是說,在運算的過程中都不允許有超出範圍的數出現)。
輸入規範
輸入檔案的第一列是1個整數n,代表接下來有多少組測試資料。
每組測試資料一列。每列的第一個整數 p(0 < p 100),代表要做運算的數有多少個。接下來有p個正整數,每列的最後一個數(即p+1個)為目標數。所有的數都小於32000,而每個數字間以一個空格分開(請參考輸入範例)。
輸出規範
每列測試資料輸出一列運算式,使得輸入的p個正整數運算的結果等於目標數。如果找不到這樣的運算式,請輸出"無解"。如果有多組運算式可以達成任務,請輸出任何一組均可。請參考輸出範例。
輸入範例(test4.txt)
3
3 5 7 4 3
2 1 1 2000
5 12 2 5 1 2 4
輸出範例(result4.txt)
5+7/4=3
無解
12-2/5*1*2=4
Private Sub Form_Load()
回覆刪除Dim z As String
Dim x As Integer
Dim y As Integer
Dim ans As Boolean
Open App.Path & "\in.txt" For Input As #1
Input #1, x
For i = 1 To x
Line Input #1, a
ta = Split(a, " ")
Open App.Path & "\t1out.txt" For Output As #2
Print #2, ta(1), ta(1)
Close #2
For j = 2 To UBound(ta) - 1
Open App.Path & "\t1out.txt" For Input As #2
Open App.Path & "\t2out.txt" For Output As #3
Do While Not EOF(2)
Input #2, y, z
If Not (y + ta(j) > 32000) And Not (y + ta(j) < -32000) Then Print #3, y + ta(j), z & "+" & ta(j)
If Not (y - ta(j) > 32000) And Not (y - ta(j) < -32000) Then Print #3, y - ta(j), z & "-" & ta(j)
If Not (y * ta(j) > 32000) And Not (y * ta(j) < -32000) Then Print #3, y * ta(j), z & "*" & ta(j)
If Not (y / ta(j) > 32000) And Not (y / ta(j) < -32000) Then Print #3, y / ta(j), z & "/" & ta(j)
Loop
Close #2
Close #3
'交換兩個檔案
Open App.Path & "\t1out.txt" For Output As #2
Open App.Path & "\t2out.txt" For Input As #3
Do While Not EOF(3)
Line Input #3, All
Print #2, All
Loop
Close #2
Close #3
Next j
Open App.Path & "\t1out.txt" For Input As #2
Do While Not EOF(2)
Input #2, y, z
ans = False
If y = ta(UBound(ta)) Then
Print z & " = " & y
ans = True
Exit Do
End If
Loop
If ans = False Then Print "無解"
Close #2
Next i
End Sub
Private Sub Form_Load()
回覆刪除Dim z As String
Dim x As Integer
Dim y As Integer
Dim ans As Boolean
Open App.Path & "\in.txt" For Input As #1
Input #1, x
For i = 1 To x
Line Input #1, a
ta = Split(a, " ")
Open App.Path & "\t1out.txt" For Output As #2
Print #2, ta(1), ta(1)
Close #2
For j = 2 To UBound(ta) - 1
Open App.Path & "\t1out.txt" For Input As #2
Open App.Path & "\t2out.txt" For Output As #3
Do While Not EOF(2)
Input #2, y, z
If Not (y + ta(j) > 32000) And Not (y + ta(j) < -32000) Then Print #3, y + ta(j), z & "+" & ta(j)
If Not (y - ta(j) > 32000) And Not (y - ta(j) < -32000) Then Print #3, y - ta(j), z & "-" & ta(j)
If Not (y * ta(j) > 32000) And Not (y * ta(j) < -32000) Then Print #3, y * ta(j), z & "*" & ta(j)
If Not (y / ta(j) > 32000) And Not (y / ta(j) < -32000) Then Print #3, y / ta(j), z & "/" & ta(j)
Loop
Close #2
Close #3
'交換兩個檔案
Open App.Path & "\t1out.txt" For Output As #2
Open App.Path & "\t2out.txt" For Input As #3
Do While Not EOF(3)
Line Input #3, All
Print #2, All
Loop
Close #2
Close #3
Next j
Open App.Path & "\t1out.txt" For Input As #2
Do While Not EOF(2)
Input #2, y, z
ans = False
If y = ta(UBound(ta)) Then
Print z & " = " & y
ans = True
Exit Do
End If
Loop
If ans = False Then Print "無解"
Close #2
Next i
End Sub