2012年3月3日 星期六

龜兔賽跑

「99年烏龜和兔子盃有一跑步大賽」開始了!這次還是有好多烏龜和兔子的朋友來參加,今年的大賽和往年相同,路程全長一公里,每次由烏龜組和兔子組各推派一個選手參加比賽,在槍聲響起後就可以自由地跑,先到達終點者獲勝。因為是友誼賽,所以只要再同一分鐘到達者,就算平手。
輸入檔說明:輸入資料有超過1組的比賽數據,每組3行資料,資料的第1行是一個整數n,範圍從0到20。當n等於0時,代表資料結束。每組資料的第2行有n個整數,由空白分開,範圍從-1000到1000,第i個整數代表烏龜組選手在第i分鐘裡所跑的距離,單位是公尺,如果為正數就是代表往前跑,負數就是往回跑。第3行和第2行相同,不過代表的是兔子組選手的資料。注意::當有任何一方已到達終點時,後面資料應該被忽略。
輸出檔說明:每1組比賽數據應有1組輸出結果,每1組輸出結果應該只有1行字串,代表這一組比賽是誰獲勝(跑的距離先等於或大於1000),烏龜組獲聖則輸出Turtle,兔子組就是Rabbit,平手的話就輸出Same,如果資料結束時比賽還未結束(雙方都還沒到達終點),就輸出Unknow。

輸入範例:
4
200 400 600 600
400 -200 800 -200
6
200 200 200 200 200 200
300 300 0 0 300 300
2
100 100
400 -200
0
輸出範例:
Same
Turtle
Unknow

4 則留言:

  1. 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 x(20), y(20), r, t As Integer
    Do
    Input #1, n
    For i = 1 To n
    Input #1, x(i)
    Next
    For i = 1 To n
    Input #1, y(i)
    Next
    r = 0
    t = 0
    For i = 1 To n
    t = t + x(i)
    r = r + y(i)
    If r >= 1000 And t < 1000 Then Print #2, "Rabbit": Exit For
    If t >= 1000 And r < 1000 Then Print #2, "Turtle": Exit For
    If r >= 1000 And t >= 1000 Then Print #2, "Same": Exit For
    Next
    If n = 0 Then Exit Do
    If r < 1000 And t < 1000 Then Print #2, "Unknow"
    Loop Until n = 0
    Close
    Close
    End
    End Sub

    回覆刪除
  2. 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 x(20), y(20), a, b As Integer
    Do Until EOF(1)
    Input #1, n
    If n = 0 Then Exit Do
    For i = 1 To n
    Input #1, x(i)
    Next i
    For i = 1 To n
    Input #1, y(i)
    Next i
    a = 0
    b = 0
    For j = 1 To n
    a = a + x(j)
    b = b + y(j)
    If (a >= 1000) And (b < 1000) Then Print #2, "Turtle": Exit For
    If (a < 1000) And (b >= 1000) Then Print #2, "Rabbit": Exit For
    If (a >= 1000) And (b >= 1000) Then Print #2, "Same": Exit For
    Next j
    If (a < 1000) And (b < 1000) Then Print #2, "Unknow"
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim x(10), y(10) As Integer
    Do Until EOF(1)
    Input #1, n
    If n = 0 Then Exit Do
    For i = 1 To n
    Input #1, x(i)
    Next i
    For i = 1 To n
    Input #1, y(i)
    Next i
    a = 0
    b = 0
    For i = 1 To n
    a = a + x(i)
    b = b + y(i)
    If a >= 1000 And b < 1000 Then Print #2, "turtle": Exit For
    If b >= 1000 And a < 1000 Then Print #2, "rabbit": Exit For
    If a >= 1000 And b >= 1000 Then Print #2, "same": Exit For
    Next i
    If a < 1000 And b < 1000 Then Print #2, "unknow"
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    Input #1, n
    If n = 0 Then Exit Do
    Line Input #1, q
    Line Input #1, z
    t = Split(q)
    r = Split(z)
    For i = 0 To Val(n) - 1
    howlong = howlong + Val(t(i))
    howlong1 = howlong1 + Val(r(i))
    If howlong1 >= 1000 And howlong >= 1000 Then
    Print #2, "same"
    Exit For
    ElseIf howlong >= 1000 And howlong1 < 1000 Then
    Print #2, "turtle"
    Exit For
    ElseIf howlong1 >= 1000 And howlong < 1000 Then
    Print #2, "rabbit"
    Exit For
    End If
    Next i
    If howlong1 < 1000 And howlong < 1000 Then Print #2, "Unknow"
    howlong = 0: howlong1 = 0
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除