如何实现求一组数字中连续一样数值的总数最大值?

010052 2003-02-04 12:00:25
已经声明一个a(12)的数组,要求算出连续同样数值的个数的最大值,a只有3种可能(0,1,3),如本例中要求出的0的连续最大值应该是3,1的连续最大值应该是3,3的连续最大值应该是2!需要详细代码及编程思路!
a(0) = 0
a(1) = 0
a(2) = 1
a(3) = 1
a(4) = 1
a(5) = 3
a(6) = 3
a(7) = 1
a(8) = 1
a(9) = 1
a(10) = 0
a(11) = 0
a(12) = 0
...全文
241 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
chanet 2003-02-08
  • 打赏
  • 举报
回复
代码还是自己写好一点!~~~

给一点我的思路给你吧!

主要是用一个i变量来临存连续的个数和

b变量来存连续的个数,然后用循环来查,

如果第一个和第二个的值是相同的话,i=i+1,否则b=i (i然后就重新算过)

...
mwjx 2003-02-07
  • 打赏
  • 举报
回复
思路如下:
for i=0 to 11
count_0[i]=0
next
for i=0 to 11
if a[i]=thevalue then
count_0[flag]+=1
else
flag+=1
end if
next

结果:
intmax=0
for i=0 to 11
if count_0[i]<>0 then
if intmax<count_0[i] then
intmax=count_0[i]
end if
end if
next
Tenner 2003-02-07
  • 打赏
  • 举报
回复
Dim i As Long
Dim t As Long
Dim count As Long
Dim final As Long
t = a(LBound(a()))
For i = LBound(a()) + 1 To UBound(a())
If a(i) = t Then
count = count + 1
Else
final = count
count = 0
t = a(i)
End If
Next
If count > final Then final = count

运行结束后 final 中的值就是你想要的
starbaby 2003-02-07
  • 打赏
  • 举报
回复
晕,我也没看明白楼主的意思,不好意思了~~~~
佛的光辉 2003-02-07
  • 打赏
  • 举报
回复
不好意思错了一行代码,将
if x(i)=my then
lCount1=lCount1+1
else
lCount2=lCount1
lCount1=0
end if
改为
if x(i)=my then
lCount1=lCount1+1
else
if lCount1>0 then lCount2=lCount1
lCount1=0
end if
佛的光辉 2003-02-07
  • 打赏
  • 举报
回复
Function countofsame(ByVal my As long, x() As long) As long
dim lCount1 as long,lCount2 as long
dim i as integer
lCount1=0
lCount2=0
For i=LBound(x()) to UBound(x())
if x(i)=my then
lCount1=lCount1+1
else
lCount2=lCount1
lCount1=0
end if
Next
if lCount2>lCount1 then
countofsame=lCount2
else
countofsame=lCount1
end if
End Function
northwolves 2003-02-07
  • 打赏
  • 举报
回复
试试下面的代码,不知我理解的对否?




Private Sub Command1_Click() 'check the function "countofsame"
Dim a(1000) As String
Randomize
For i = 0 To 1000
a(i) = Int(5 * Rnd)
List1.AddItem a(i) 'show the array in list1
Next
MsgBox countofsame("1", a())
End Sub

Function countofsame(ByVal mystr As String, x() As String) As Integer
countofsame = 0
Dim temp As Integer, i As Integer
i = LBound(x())
temp = 0
Do While i <= UBound(x())
If x(i) = mystr Then
temp = temp + 1
countofsame = IIf(countofsame >= temp, countofsame, temp)
Else
temp = 0
End If
i = i + 1
Loop
End Function
绝缘 2003-02-04
  • 打赏
  • 举报
回复
没懂,UP

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧