請檢查一個數列中,尋找任兩個數字相加後的和是否會等於輸入的檢查值。
例如,有一個數列為8,20,27,17,13,28,35,31,若輸入的檢查值為48,會找到20及28兩個數,相加的和為48;反之若輸入的檢查值為50,則會找不到配對的數字。
輸入說明:
輸入之第一列為下一列數列中的個數,第二列為數列資料。每個數字與數字間的區隔為一個空白符號。第三列及以後的資料為輸入的檢查值,每一列有一個檢查值,當為0時表示結束。
輸入範圍:
輸入的數列最少有2個數字,最多不超過100個。每個數列中的數字皆大於0,小於1000,且不重覆。
輸入範例:in.txt
8
8 20 27 17 13 28 35 31
48
50
59
0
輸出說明:
每個檢查值有一個輸出結果,若有匹配的檢查值,則輸出為1,反之為-1。
輸出範例:out.txt
1
-1
1
Dim strr As String
回覆刪除Dim find As Boolean
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, strr
Input #1, strr
num = Split(strr, " ")
Do
Input #1, keynum
If keynum = 0 Then Exit Do
For i = 0 To UBound(num)
For ii = 0 To UBound(num)
If Val(num(i)) + Val(num(ii)) = keynum Then find = True: Exit For
Next
If find = True Then Exit For
Next
If find = True Then Print #2, 1
If find = False Then Print #2, -1
find = False
Loop
Close #2
Close #1
End
End Sub
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, c
Line Input #1, x
x = Split(x)
Do
Input #1, a
If a = 0 Then Exit Do
For i = 0 To UBound(x) - 1
For j = 1 To UBound(x)
z = Val(x(i)) + Val(x(j))
If z = a Then
Print #2, "1"
Exit For
End If
Next
If z = a Then Exit For
Next
If z <> a Then Print #2, "-1"
Loop
Close #2
Close #1
End
End Sub
Dim b As Boolean
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
Line Input #1, t
s = Split(t, " ")
Do
b = False
Input #1, z
If z = 0 Then Exit Do
For k = 0 To x - 1
For m = 0 To x - 1
If Val(s(k)) + Val(s(m)) = z Then b = True
Next
Next
If b = True Then Print #2, "1" Else Print #2, "-1"
Loop
Close
Close
End
End Sub