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, mycount For ai = 1 To mycount Line Input #1, temp strr = Split(temp, " ") For bi = 0 To UBound(strr) tempp = 0 For i = bi To UBound(strr) tempp = tempp + Val(strr(i)) If tempp > maxx Then maxx = tempp Next Next Print #2, maxx: maxx = 0 Next Close #2 Close #1 End End Sub
Dim max 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 Input #1, n For t = 1 To Val(n) Line Input #1, x: max = 0 a = Split(x) For i = 0 To UBound(a) ans = Val(a(i)) For j = i + 1 To UBound(a) ans = ans + Val(a(j)) If ans > max Then max = ans Next j Next i Print #2, max Next t Close #2 Close #1 End End Sub
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, mycount
For ai = 1 To mycount
Line Input #1, temp
strr = Split(temp, " ")
For bi = 0 To UBound(strr)
tempp = 0
For i = bi To UBound(strr)
tempp = tempp + Val(strr(i))
If tempp > maxx Then maxx = tempp
Next
Next
Print #2, maxx: maxx = 0
Next
Close #2
Close #1
End
End Sub
例如:1, 2, -3, 4, 5 這一數列,最大連續元素和是4+5=9。
回覆刪除'---
輸入範例:
2
1 2 3 4 5 >> 1+2+3+4+5 = 15
10 -5 7 6 -1 -3 >> 6 + 7 =18?
輸出範例:
15
18
10 -5 7 6 -1 -3 >> 10 + -5 + 7 + 6 =18
回覆刪除是要加起來最大,並不是不找負數。
喔喔~~連續元素唷...
刪除會錯意,以為是 連續數字....
Dim max 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
Input #1, n
For t = 1 To Val(n)
Line Input #1, x: max = 0
a = Split(x)
For i = 0 To UBound(a)
ans = Val(a(i))
For j = i + 1 To UBound(a)
ans = ans + Val(a(j))
If ans > max Then max = ans
Next j
Next i
Print #2, max
Next t
Close #2
Close #1
End
End Sub