Problem3:最大連續元素和(15%)
給一串數列,有n 個整數,請寫一個程式,找出這個數列中,連續元素相加的最大值。
例如:1, 2, -3, 4, 5 這一數列,最大連續元素和是4+5=9。
輸入說明:
第一行的數字,代表有幾組測試資料,第二行開始的每一行即為一筆測試資料。每一筆
測試資料是不定個數的整數數列,以空格分開數字。數字的範圍為[-10000, 10000]間的整數。
輸出說明:
對每一筆測試資料,以一行輸出最大連續數值和。
輸入範例:
2
1 2 3 4 5
10 -5 7 6 -1 -3
輸出範例:
15
18
Dim s
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, t
For i = 1 To t
List1.Clear
Line Input #1, n
s = Split(n)
Call A1
Next
Close
Close
End
End Sub
Sub A1()
Dim w$
For i = 0 To UBound(s)
w = 0
For j = i To UBound(s)
w = Val(w) + Val(s(j))
If InStr(w, "-") <> 0 Then w = Replace(w, "-", "*")
List1.AddItem A2(w)
Next
Next
If InStr(List1.List(List1.ListCount - 1), "*") <> 0 Then List1.List(List1.ListCount - 1) = Replace(List1.List(List1.ListCount - 1), "*", "-")
Print #2, A3(List1.List(List1.ListCount - 1))
End Sub
Function A2(a)
Do While Len(a) < 9
a = "0" & a
Loop
A2 = a
End Function
Function A3(a)
Do While Left(a, 1) = 0
a = Right(a, Len(a) - 1)
Loop
A3 = a
End Function
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, N
For ii = 1 To N
Line Input #1, X
Y = Split(X)
Max = 0
For i = 0 To UBound(Y) - 1
A = 0
For j = i + 1 To UBound(Y)
If A = 0 Then A = Val(Y(i)) + Val(Y(j)) Else A = A + Val(Y(j))
If A > Max Or Max = 0 Then Max = A
Next j
Next i
Print #2, Max
Next ii
Close #2
Close #1
End
End Sub
arro的程式碼錯的很嚴重
回覆刪除柯佑的程式碼有一個地方沒考慮到
by某屆程式選手前三
學長,我的程式沒有錯誤。
回覆刪除請將list1.sorted的屬性設為true
還是有錯喔
回覆刪除你們兩個還有一個地方沒有考慮到
話說你怎麼知道我是學長...
找到了
回覆刪除我把
If InStr(w, "-") <> 0 Then w = Replace(w, "-", "*")
換成
If InStr(w, "-") <> 0 Then w = 20000+w
If InStr(List1.List(List1.ListCount - 1), "*") <> 0 Then List1.List(List1.ListCount - 1) = Replace(List1.List(List1.ListCount - 1), "*", "-")
Print #2, A3(List1.List(List1.ListCount - 1))
換成
Print #2, A3(List1.List(List1.ListCount - 1)-20000)
這樣就沒錯了吧 :)
作者已經移除這則留言。
回覆刪除怎麼越改越錯?
回覆刪除你連原本的解答都錯了還是我沒有弄好?
轉上來改錯地方
回覆刪除Dim s
Private Sub Form_Load()
'Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, t
For i = 1 To t
List1.Clear
Line Input #1, n
s = Split(n)
Call A1
Next
Close
Close
'End
End Sub
Sub A1()
Dim w$
For i = 0 To UBound(s)
w = 0
For j = i To UBound(s)
w = Val(w) + Val(s(j))
'If InStr(w, "-") <> 0 Then w = w + 20000
List1.AddItem A2(w + 20000)
Next
Next
'If InStr(List1.List(List1.ListCount - 1), "*") <> 0 Then List1.List(List1.ListCount - 1) = List1.List(List1.ListCount - 1) - 20000
Print #2, A3(List1.List(List1.ListCount - 1) - 20000)
End Sub
Function A2(a)
Do While Len(a) < 9
a = "0" & a
Loop
A2 = a
End Function
Function A3(a)
Do While Left(a, 1) = 0
a = Right(a, Len(a) - 1)
Loop
A3 = a
End Function
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, N
For ii = 1 To N
Line Input #1, X
Y = Split(X)
Max = 0
For i = 0 To UBound(Y)
A = Val(Y(i))
If Max = 0 Or Max < A Then Max = A
Next i
For i = 0 To UBound(Y) - 1
A = 0
For j = i + 1 To UBound(Y)
If A = 0 Then A = Val(Y(i)) + Val(Y(j)) Else A = A + Val(Y(j))
If A > Max Or Max = 0 Then Max = A
Next j
Next i
Print #2, Max
Next ii
Close #2
Close #1
End
End Sub
單單一個沒有考慮到
ok你們兩個應該都對了
回覆刪除我只是因為我學弟叫我教他某一屆的題目他才po這邊的題目給我看
才會路過這邊的
這樣看來感覺你們應該是高二的
總之繼續加油
也希望你們可以得到前三