給一串數列,有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 a(1000) As Integer
回覆刪除Dim b(1000) As Integer
Dim ans(100) As Integer
Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
p = 1
t = 1
Do While Not EOF(1)
Line Input #1, aa
aaa = Split(aa)
For i = 0 To UBound(aaa)
a(i + 1) = aaa(i)
p = p + 1
Next i
k = 1
For i = 1 To p - 1
b(k - 1) = 0
For j = i To p - 1
b(k) = b(k - 1) + a(j)
If j = p - 1 Then
k = k + 2
Else
k = k + 1
End If
Next j
Next i
maxK = k - 2
ans(t) = 0
For i = 1 To maxK
If b(i) > ans(t) Then ans(t) = b(i)
Next i
t = t + 1
Loop
For i = 1 To n
Print #2, ans(i)
Next i
Close #2
Close #1
End Sub
老師,這題雖然硬著頭皮做完了,
可是我覺得應該還是有很多地方是錯的!
所以還煩請老師指教!
小白好,
回覆刪除看得好花哦,但是,看來是對的說。
你用了雙迴圈而將每次的累加都用另一個陣列來存起來,
於是,你有1,1+2,1+2+3,...,1+2+3+...+n,
2, 2+3, 2+3+4, ..., 2+3+4+...+n,
...
(n-1),(n-1)+n,
n
你存了這個b陣列,再去將b陣列中最大的給找出來。
答案應該是正確的。
你用幾組輸入去試試,並將輸出給po出來吧。
不錯哦,有後來居上的態勢哦,加油。全組都加油哦。
我用檔案的方式,
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #4
Input #1, n
For i = 1 To n
Line Input #1, x
a = Split(x, " ")
Open App.Path & "\tempin.txt" For Output As 2
Print #2, a(0)
Close #2
For j = 1 To UBound(a)
Call mysum(a(j), j)
Next j
Open App.Path & "\tempin.txt" For Input As #2
maxb = 0
Do While Not EOF(2)
Input #2, b
If b > maxb Then maxb = b
Loop
Close #2
Print #4, maxb
Next i
Close #4
End Sub
Sub mysum(y, n)
Dim a(1000)
Open App.Path & "\tempin.txt" For Input As #2
Open App.Path & "\tempout.txt" For Output As #3
For i = 1 To n
For j = 1 To i
c = c + 1
Input #2, a(c)
Print #3, a(c)
Next j
Next i
Print #3, y
For i = c - n + 1 To c
Print #3, a(i) + y
Next i
Close #2
Close #3
' swap tempin.txt , tempout.txt
Open App.Path & "\tempout.txt" For Input As #2
Open App.Path & "\tempin.txt" For Output As #3
Do While Not EOF(2)
Input #2, z
Print #3, z
Loop
Close #2
Close #3
End Sub
in.txt -->
3
10 -5 7 6 -1 -3
1 2 3 4 5
3 -9 2 3 5 10 -8 12 -3 -5 4
out.txt -->
18
15
24
Private Sub Form_Load()
回覆刪除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
a = Split(x, " ")
For i = 0 To UBound(a)
For j = i To UBound(a)
Max = 0
For k = 0 To j
Max = Max + Val(a(k))
Next k
If Max > ans Then ans = Max
Next j
Next i
Print #2, ans
Next ii
Close #2
Close #1
End Sub
阿瑋好,
回覆刪除如剛剛的檢討,這題是錯的。
大家都要學會「自己設定輸入資料」,
要學會如何來找碴,各種很特殊,卻正確的輸入資料。
尤其是「邊界值」或是0之類的。
(如果不是比賽用,還要會去找出可能的「錯誤的輸入」呢)
Private Sub Form_Load()
回覆刪除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
ans = 0
Line Input #1, x
a = Split(x, " ")
For i = 0 To UBound(a)
For j = i To UBound(a)
Max = 0
For k = i To j
Max = Max + Val(a(k))
Next k
If Max > ans Then ans = Max
Next j
Next i
Print #2, ans
Next ii
Close #2
Close #1
End Sub
更改過後 i to j 才對
阿瑋好,
回覆刪除ok。
Dim ans As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
For q = 1 To x
Line Input #1, e
num = Split(e)
Max = 0
For i = 0 To UBound(num)
ans = 0
For j = i To UBound(num)
ans = ans + Val(num(j))
If ans > Max Then Max = ans
Next j
Next i
Print #2, Max
Next q
Close #2
Close #1
End Sub