「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
Dim TL(99), RL(99) As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do
Input #1, x
T = 0: R = 0
If x = 0 Then Exit Do
For i = 1 To x
Input #1, TL(i)
Next i
For i = 1 To x
Input #1, RL(i)
Next i
For i = 1 To x
T = T + TL(i)
If T >= 1000 Then
TT = i
Exit For
End If
Next i
For i = 1 To x
R = R + RL(i)
If R >= 1000 Then
RT = i
Exit For
End If
Next i
If T And R < 1000 Then
Print #2, "Unknow"
Else
If TT < RT Then
Print #2, "Turtle"
ElseIf TT = RT Then
Print #2, "Same"
Else
Print #2, "Ribbit"
End If
End If
Loop
Close #1
Close #2
End Sub
阿瑋好,
回覆刪除這一行
if T and R<1000 then
是錯的方式。
它是
if (T) and (R<1000) then
但是,T是true 還是 false呢?
答案是,如果T是0, 則是 false,其餘,都是true.
Dim TL(99), RL(99) As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do
Input #1, x
T = 0: R = 0
If x = 0 Then Exit Do
For i = 1 To x
Input #1, TL(i)
Next i
For i = 1 To x
Input #1, RL(i)
Next i
For i = 1 To x
T = T + TL(i)
If T >= 1000 Then
TT = i
Exit For
End If
Next i
For i = 1 To x
R = R + RL(i)
If R >= 1000 Then
RT = i
Exit For
End If
Next i
If T < 1000 And R < 1000 Then
Print #2, "Unknow"
Else
If TT < RT Then
Print #2, "Turtle"
ElseIf TT = RT Then
Print #2, "Same"
Else
Print #2, "Ribbit"
End If
End If
Loop
Close #1
Close #2
End Sub
想說上面的範例都對就沒注意到了ˊˋ
兔子是rabbit吧? 如果比賽時,出現這樣的小問題,怎麼辦?
回覆刪除隊長英文單字打錯了= ="
回覆刪除我看他的範例的說~"~
難怪我覺得怪怪的-..-
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 Sub
ReDim T(x), R(x)
For j = 0 To x - 1
Input #1, T(j)
Next j
For j = 0 To x - 1
Input #1, R(j)
Next j
Turtle = 0
Rabbit = 0
For j = 0 To x - 1
Turtle = Turtle + T(j)
Rabbit = Rabbit + R(j)
If Turtle >= 1000 Or Rabbit >= 1000 Then Exit For
If j = x - 1 And Turtle < 1000 And Rabbit < 1000 Then Print #2, "Unknow"
Next j
If Turtle >= 1000 And Rabbit < 1000 Then Print #2, "Turtle"
If Rabbit >= 1000 And Turtle < 1000 Then Print #2, "Rabbit"
If Rabbit >= 1000 And Turtle >= 1000 Then Print #2, "Same"
Loop
Close #2
Close #1
End Sub
錯誤改好了...ORZ
這幾天不在家 很難用到電腦
會完成的比較遲 不好意思摟
Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do
t = 0: r = 0
ReDim rr(10), tt(10) As Integer
Input #1, x
If x = 0 Then Exit Sub
For i = 1 To x
Input #1, tt(i)
Next i
For i = 1 To x
Input #1, rr(i)
Next i
For i = 1 To x
t = t + tt(i)
r = r + rr(i)
If t >= 1000 And r >= 1000 Then Print #2, "same": Exit For
If t >= 1000 And r < 1000 Then Print #2, "Turtle": Exit For
If t < 1000 And r >= 1000 Then Print #2, "Rabbit": Exit For
If i = x And t < 1000 And r < 1000 Then Print #2, "Unknow": Exit For
Next i
Loop
Close #1
Close #2
End Sub
高仔好,
回覆刪除ok.
Y揚好,
你預設不會超過10步,這點有些危險,如果評審的資料檔,有超過10步的,你的程式就會出錯了。
exit sub
->
exit do
Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do
ans = ""
Trun = 0
Rrun = 0
Input #1, aaa
If aaa = 0 Then Exit Do
Line Input #1, Tstr
Line Input #1, Rstr
Tru = Split(Tstr, " ")
Rru = Split(Rstr, " ")
For i = 0 To aaa - 1
Trun = Trun + Val(Tru(i))
Rrun = Rrun + Val(Rru(i))
If Rrun >= 1000 And Trun >= 1000 Then
ans = "Same"
Exit For
End If
If Trun >= 1000 Then
ans = "Turtle"
Exit For
End If
If Rrun >= 1000 Then
ans = "Rabbit"
Exit For
End If
If Trun < 1000 And Rrun < 1000 Then
ans = "Unknow"
End If
Next i
Print #2, ans
Loop
Close #2
Close #1
End Sub
小白好,
回覆刪除1這題程式結果應該是ok。
2程式中你用了exit do和exit for,都是不必要的「非結構化」。
例如,先
input #1,aaa
再來
do while aaa <> 0
...
loop
還有
if r>1000 and t>1000 then
....
elseif r>1000 then
...
elseif t>1000 then
...
else
ans="Unkonw"
endif