小明請小華猜出他心理想的一個數字,這個數字為[1, 100]間的整數。猜測的規則為:每一回華猜測一個數字,小明則回應小華猜的太高(too high),太低(too low),或是猜中(right on),猜中後立即結束遊戲。因為過程中小明可能會說謊,你必須寫一個程式,在每次結束之後,驗証明他的回應是否都正確。
輸入說明:
輸入中含有多次遊戲的記錄。在每一次遊戲中會包含許多次的猜測及回應的過程。每一次遊戲的最後都必須猜中才能結束。在最後一組遊戲後,由僅含有0 的一列代表輸入結束。
輸出說明:
針對每一次的遊戲,程式以一行輸出小明是否有說謊。如果這次遊戲有說謊則輸出0,
沒有說謊則輸出1。
輸入範例:
5
too high
3
too high
1
too low
2
right on
33
too low
34
too high
32
right on
0
輸出範例:
1
0
Private Sub Form_Load()
回覆刪除Open App.Path & "/in.txt" For Input As #1
Open App.Path & "/out.txt" For Output As #2
Do While Not EOF(1)
Do
Input #1, n
If n = 0 Then Exit Sub
Input #1, m
If m <> "right on" Then
If m = "too high" Then Max = n
If m = "too low" Then Min = n
Else
If n > Min And n < Max Then
Print #2, "1"
Else
Print #2, "0"
End If
End If
Loop While m = "right on"
Loop
Close #2
Close #1
End Sub
高仔好,
回覆刪除程式OK。
Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do While Not EOF(1)
Input #1, x
If x = 0 Then Exit Do
Input #1, y
If y = "too high" Then Max = x
If y = "too low" Then Min = x
If y = "right on" Then
If Min < x And x < Max Then
Print #2, 1
Else
Print #2, 0
End If
Max = 0: Min = 999
End If
Loop
Close #2
Close #1
End Sub
Dim max As Integer, min As Integer, s As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
max = 100: min = 0
Do
Input #1, x
If x = 0 Then Exit Sub
Input #1, st
Select Case st
Case "too high"
If max > x Then max = x
Case "too low"
If min < x Then min = x
Case "right on"
If x < max And x > min Then Print #2, "1" Else Print #2, "0"
max = 100: min = 0
End Select
Loop
Close #2
Close #1
End Sub