在1楼数列的基础上,检查优化得到最大连续个数=117【2 - 118】
1楼数列:1,2,3,4,5,6,7,14,21,28,35,42,49,56,63,70,77,84,91,98
优化后:1,2,3,4,5,6,7,14,21,28,35,42,49,56,62,68,74,80,86,92
- Sub test() 'by kagawa 2017/7/5
- Dim a&(), b, i&, i2&, k&, n&, n1&, t&, t2&
- b = [{1,2,3,4,5,6,7,14,21,28,35,42,49,56,63,70,77,84,91,98}] '原始模板
- For n1 = 2 To 20 '从第2个数开始检查替换
- ReDim a(1 To 20) '重置数组a
- For i = 1 To n1
- a(i) = b(i) '根据模板填写前n1个数
- Next
- For n = n1 To 20 '循环检查填后面的数
- t2 = f(a, n - 1): k = 0 '计算当前最大连续数t 以及没有变化的计数k归零
- For i = a(n - 1) + 1 To 1000 '以前一个数+1开始检查替换
- a(n) = i: t = f(a, n) '替换 并统计最大连续个数
- If t > t2 Then t2 = t: i2 = i Else k = k + 1: If k > 5 Then Exit For
- '更新连续个数的最大值t2 如果此值不能增大则连续5个数都不增大则提前结束
- Next
- a(n) = i2 '更新为第一个有效最大值时的数字
- Next
- Cells(n1, 1) = t2 '输出连续最大个数
- Cells(n1, 2).Resize(, n - 1) = a '输出这一组数
- Next
- End Sub
- Function f&(a, n&) '统计最大连续个数的自定义函数 解释略
- Dim i&, i2&, k&, k2&, n1&, n2&, t&
- n1 = a(1) + a(1): n2 = a(n) + a(n)
- ReDim b(n1 To n2) As Boolean
- For i = 1 To n
- t = a(i)
- For i2 = 1 To n
- b(t + a(i2)) = True
- Next
- Next
- For i = n1 To n2
- If b(i) Then k = k + 1 Else If k > k2 Then k2 = k: k = 0
- Next
- If k > k2 Then k2 = k
- f = k2
- End Function
复制代码
|