請檢查一個數列中,尋找任兩個數字相加後的和是否會等於輸入的檢查值。
例如,有一個數列為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
Private Sub Form_Load()
回覆刪除Me.Hide
Dim ans As Double
Dim a(100) As Integer
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
For i = 1 To n
Input #1, x
a(i) = x
Next
Do
Input #1, y
For i = 1 To n
ans = 0
For j = 1 To n
ans = a(i) + a(j)
If ans = y Then Print #2, "1": Exit For
Next
If ans = y Then Exit For
Next
If y = 0 Then Exit Do
If ans <> y Then Print #2, "-1"
Loop Until y = 0
Close
Close
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
Dim a(100)
Input #1, n
For i = 1 To n
Input #1, c
a(i) = c
Next i
Do
Input #1, b
If b = 0 Then Exit Do
For i = 1 To n
ans = 0
For j = 1 To n
ans = a(i) + a(j)
If ans = b Then
Print #2, "1": Exit For
End If
Next j
If ans = b Then: Exit For
Next i
If ans <> b Then
Print #2, "-1"
End If
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
Dim a(100) As Integer
Input #1, n
For i = 1 To n
Input #1, a(i)
Next i
Do
Input #1, x
If x = 0 Then Exit Do
For i = 1 To n
ans = 0
For j = 1 To n
ans = a(i) + a(j)
If ans = x Then Print #2, "1": Exit For
Next j
If ans = x Then Exit For
Next i
If ans <> x Then Print #2, "-1"
Loop
Close #2
Close #1
End
End Sub
作者已經移除這則留言。
回覆刪除Dim a() As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
ReDim a(Val(n))
For i = 1 To Val(n)
Input #1, x
a(i) = Val(x)
Next i
Do While Not EOF(1)
Input #1, y
If Val(y) = 0 Then End
b = -1
For i = 1 To Val(n)
g = Val(y) - a(i)
For j = 1 To Val(n)
If i <> j Then
If a(j) = g Then
b = 1
Print #2, b
Exit For
End If
End If
Next j
If b = 1 Then Exit For
Next i
If b = -1 Then Print #2, b
Loop
Close #2
Close #1
End
End Sub