2012年11月6日 星期二

龜兔賽跑

「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

3 則留言:

  1. Dim num As Integer
    Dim turtle(9), rabbit(9), turtleCom, rabbitCom As Integer
    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, num
    If num = 0 Then Exit Do

    For i = 1 To num
    Input #1, turtle(i)
    Next

    For ii = 1 To num
    Input #1, rabbit(ii)
    Next

    For iii = 1 To num
    turtleCom = turtleCom + turtle(iii)
    rabbitCom = rabbitCom + rabbit(iii)
    If turtleCom >= 1000 And rabbitCom < 1000 Then Print #2, "Turtle": Exit For
    If turtleCom < 1000 And rabbitCom >= 1000 Then Print #2, "Rabbit": Exit For
    If turtleCom >= 1000 And rabbitCom >= 1000 Then Print #2, "Same": Exit For
    Next
    If turtleCom < 1000 And rabbitCom < 1000 Then Print #2, "Unknow"
    turtleCom = 0
    rabbitCom = 0
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Dim ax, ay, x(), y() As Integer
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    ax = 0
    ay = 0
    i = 0
    Input #1, n
    If n = 0 Then Exit Do
    ReDim x(n)
    For j = 0 To n - 1
    Input #1, x(j)
    Next
    ReDim y(n)
    For j = 0 To n - 1
    Input #1, y(j)
    Next
    'For k = 0 To n - 1
    'MsgBox x(k) & " " & y(k)
    'Next
    Do
    ax = ax + Val(x(i))
    ay = ay + Val(y(i))
    If ax >= 1000 Or ay >= 1000 Then Exit Do
    i = i + 1
    Loop Until i = n
    If ax >= 1000 Then
    If ay >= 1000 Then
    Print #2, "Same"
    Else
    Print #2, "Turtle"
    End If
    Else
    If ay >= 1000 Then
    Print #2, "Rabbit"
    End If
    End If
    If ax < 1000 And ay < 1000 Then Print #2, "Unknow"
    Loop
    Close #1
    Close #2
    End
    End Sub

    回覆刪除
  3. Dim a(), b()
    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, x
    ReDim a(x), b(x)
    If x = 0 Then Exit Do
    For i = 1 To x
    Input #1, a(i)
    Next
    For i = 1 To x
    Input #1, b(i)
    Next
    t = 0
    r = 0
    For i = 1 To x
    t = t + a(i)
    r = r + b(i)
    If t >= 1000 And r < 1000 Then Print #2, "turtle": Exit For
    If r >= 1000 And t < 1000 Then Print #2, "rabbit": Exit For
    If t >= 1000 And r >= 1000 Then Print #2, "Same": Exit For
    Next
    If t < 1000 And r < 1000 Then Print #2, "Unknow"
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除