基础编程我都做不出来 崩溃啊!求帮

frdking 2012-02-01 07:55:04
dim a(1 to 10000) as integer

a数组里面已经有数 数字范围是 1-6

要求:
求出数字在特定期间没有重复出现的间隔

比如 有 1 2 3 3 6 5 4 4 1 6

如果说在 text1中输入7 ,这个7代表有号码连续7次没有出现 就比如上面的1 有7次没有出现后 又出现了

就是说给定号码没有出现的间隔数,统计出号码是否有这样的间隔没有出现,以及有多少次这样。

...全文
95 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
KiteGirl 2012-02-02
  • 打赏
  • 举报
回复
下面的函数可以建立一个表格,你可以得到每个值每一次出现时,距离上一次出现的间隔数。
对于你给出的数据:
1 2 3 3 6 5 4 4 1 6
例程输出的数据是:
1 8
意思是:
1在上述列表中出现了2次,第1次出现在第1个值。第2次出现时距离上一次间隔是8。

对于数据:
1 2 3 3 6 5 4 4 1 6 2 3 4 1
值1的间隔表会输出
1 8 5

有个这样一个表格,剩下的相信你自己就能搞定了。


Option Explicit

Private Sub Command1_Click()
Dim tStrings() As String

Dim tValues() As Long
Dim tValues_Length As Long
Dim tValues_Index As Long

Dim tSpaces() As Long '全值间隔表(编码格式)
Dim tFilterSpaces() As Long '特定值的过滤输出间隔表
Dim tFilterSpaces_Index As Long
Dim tFilterSpaces_Length As Long

Dim tFilterValue As Long

tStrings() = Split("1 2 3 3 6 5 4 4 1 6", " ")
tFilterValue = 1 '过滤输出的目标值

tValues_Length = UBound(tStrings())

ReDim tValues(tValues_Length)

For tValues_Index = 0 To tValues_Length
tValues(tValues_Index) = CLng(tStrings(tValues_Index))
Next

tSpaces() = SpacesGetByValues(tValues()) '建立全值间隔表
tFilterSpaces() = SpacesFilterOutByValue(tSpaces(), 1) '过滤输出目标值的间隔表

tFilterSpaces_Length = UBound(tFilterSpaces())

Text1.Text = ""

For tFilterSpaces_Index = 0 To tFilterSpaces_Length
Text1.Text = Text1.Text & " " & tFilterSpaces(tFilterSpaces_Index)
Next
End Sub

Function SpacesFilterOutByValue(ByRef pSpaces() As Long, ByVal pValue As Long, Optional ByVal pValue_Min As Long = 1, Optional ByVal pValue_Max = 7) As Long()
'过滤输出特定值的间隔表
Dim tOut_Value_Spaces() As Long
Dim tOut_Value_Length As Long
Dim tValue_Bound As Long
Dim tValue_Absolute As Long
Dim tValue_Space As Long
Dim pSpaces_Index As Long
Dim pSpaces_Length As Long

tValue_Bound = pValue_Max - pValue_Min + 1

pSpaces_Length = UBound(pSpaces())

ReDim tOut_Value_Spaces(pSpaces_Length)

For pSpaces_Index = 0 To pSpaces_Length

'解码tValue_Space和tValue_Absolute解码
tValue_Space = pSpaces(pSpaces_Index) \ tValue_Bound
tValue_Absolute = pSpaces(pSpaces_Index) Mod tValue_Bound

If pValue = tValue_Absolute + pValue_Min Then

tOut_Value_Spaces(tOut_Value_Length) = tValue_Space
tOut_Value_Length = tOut_Value_Length + 1

End If
Next

ReDim Preserve tOut_Value_Spaces(tOut_Value_Length - 1)

SpacesFilterOutByValue = tOut_Value_Spaces()
End Function

Function SpacesGetByValues(ByRef pValues() As Long, Optional ByVal pValue_Min As Long = 1, Optional ByVal pValue_Max = 7) As Long()
'产生一个间隔表,它记录了每个数值(数值记录时以0开始的绝对值表示)在列表中出现时距上一次出现时的间隔次数。
Dim tOut_Value_Spaces() As Long
Dim tValues_Index As Long
Dim tValues_Length As Long
Dim tValue_Bound As Long
Dim tValue_Absolute As Long '绝对值
Dim tValue_Space As Long '间隔值

Dim tValue_HashList() As Long
Dim tValue_HashList_Length As Long
Dim tList_Length As Long

tValue_Bound = pValue_Max - pValue_Min + 1
tValue_HashList_Length = tValue_Bound - 1

ReDim tValue_HashList(tValue_HashList_Length)

tValues_Length = UBound(pValues())

ReDim tOut_Value_Spaces(tValues_Length)

For tValues_Index = 0 To tValues_Length
tValue_Absolute = pValues(tValues_Index) - pValue_Min
tValue_Space = tValues_Index - tValue_HashList(tValue_Absolute) + 1
'将tValue_Space和tValue_Absolute编码在一起
tOut_Value_Spaces(tValues_Index) = tValue_Space * tValue_Bound + tValue_Absolute
tValue_HashList(tValue_Absolute) = tValues_Index + 1
Next

SpacesGetByValues = tOut_Value_Spaces()
End Function
哈了 2012-02-01
  • 打赏
  • 举报
回复
frdking 2012-02-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 clear_zero 的回复:]
我没什么好方法,比较基础比较笨。你的数组里面不是一共就有1--6这6个数字么

你循环一遍就可以记录每个数字在数组中的位置
比如说 数字1,出现的位置 1,8,12,13,14。。。
数字2, 出现的位置 2,....

然后用这些位置和你给定的间隔数(你的例子里面是7)做个比较。 如果间隔差正好等于你的间隔数那么取出放入结果就好了
[/Quote] 2楼好方法! 1楼简直在扯蛋啊。 能不知道用循环码》?
clear_zero 2012-02-01
  • 打赏
  • 举报
回复
我没什么好方法,比较基础比较笨。你的数组里面不是一共就有1--6这6个数字么

你循环一遍就可以记录每个数字在数组中的位置
比如说 数字1,出现的位置 1,8,12,13,14。。。
数字2, 出现的位置 2,....

然后用这些位置和你给定的间隔数(你的例子里面是7)做个比较。 如果间隔差正好等于你的间隔数那么取出放入结果就好了
贝隆 2012-02-01
  • 打赏
  • 举报
回复
使用循环判断即可。

7,762

社区成员

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

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