找出无序数组中的若干数字是他们排序后能够相互之间有固定的公差1
群里有人提出的这个问题 如果不用俗套的排序 能怎么搞?Dim Range As Long : Range = 100 '公差范围
Dim MinXAs Long : MinX= 1000 '最小值
Dim i As Long i, x() As Long '数值序列
ReDim x(0 To Range - 1)'不重复的序列
For i = 0 To UBound(x)
x(i) = MinX + i
Next
Randomize
For i = 0 To UBound(x) '洗牌
Dim k As Long : k = Fix((Range - i) * Rnd() + i)
Dim t As Long : t = x(i) : x(i) = x(k) : x(k) = t
Next
ReDim Preserve x(0 To Range * 3 / 4) '断尾形成数值空档
For i = 0 To UBound(x) '打印原始数据
If (i And 15) = 0 Then
Print : Print Str(x(i));
Else
Print ", " & Str(x(i));
End If
Next
''计算并打印 公差为1的数值序列
ToleranceIn1(x(), Range, MinX)
Sub ToleranceIn1(x() As Long, ByVal Range As Long, ByVal MinX As Long)
Dim i As Long, n As Long, k As Long
Dim y() As Long : Redim y(0 To Range - 1)
For i = 0 To UBound(x)
y(x(i) - MinX) = x(i)
Next
For i = 0 To UBound(y)
If y(i) <> 0 Then
If k = 0 Then
n = n + 1
Print : Print "==============================================="
Print Str(y(i));
ElseIf ((k + 1) And 15) = 0 Then
Print : Print Str(y(i)) ;
Else
Print ", " & Str(y(i)) ;
End If
k = k + 1
Else
k = 0
End If
Next
End Sub
1076, 1027, 1075, 1048, 1049, 1013, 1096, 1074, 1051, 1079, 1037, 1091, 1092, 1089, 1040, 1042
1003, 1023, 1077, 1068, 1026, 1035, 1044, 1071, 1066, 1017, 1053, 1021, 1034, 1072, 1087, 1054
1097, 1022, 1056, 1094, 1057, 1061, 1047, 1090, 1065, 1015, 1032, 1008, 1043, 1045, 1058, 1088
1070, 1080, 1000, 1009, 1055, 1025, 1019, 1030, 1081, 1011, 1073, 1004, 1016, 1038, 1099, 1006
1014, 1086, 1002, 1093, 1050, 1039, 1084, 1020, 1069, 1036, 1031, 1095
===============================================
1000
===============================================
1002, 1003, 1004
===============================================
1006
===============================================
1008, 1009
===============================================
1011
===============================================
1013, 1014, 1015, 1016, 1017
===============================================
1019, 1020, 1021, 1022, 1023
===============================================
1025, 1026, 1027
===============================================
1030, 1031, 1032
===============================================
1034, 1035, 1036, 1037, 1038, 1039, 1040
===============================================
1042, 1043, 1044, 1045
===============================================
1047, 1048, 1049, 1050, 1051
===============================================
1053, 1054, 1055, 1056, 1057, 1058
===============================================
1061
===============================================
1065, 1066
===============================================
1068, 1069, 1070, 1071, 1072, 1073, 1074, 1075, 1076, 1077
===============================================
1079, 1080, 1081
===============================================
1084
===============================================
1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097
===============================================
1099 好麻烦啊,感觉还是直接用一个有序集合然后遍历集合或者遍历数值范围来判断哪些数值具有相同的公差。
页:
[1]