Private Sub Command2_Click() '不重复拆分
m = Val(Text1.Text) '1000
n = Val(Text2.Text) '10
m1 = Val(Text3.Text) '70
m2 = Val(Text4.Text) '150
sum1 = 0
For i = 0 To n - 1: sum1 = sum1 + m1 + i: Next
If sum1 > m Then MsgBox ("最小的连续n个数大于总数,不能拆分"): GoTo end1
sum1 = 0
For i = 0 To n - 1: sum1 = sum1 + m2 - i: Next
If sum1 < m Then MsgBox ("最大的连续n个数小于总数,不能拆分"): GoTo end1
If (m2 - m1) < n Then MsgBox ("全部区间的数量下雨拆分数,不能拆分"): GoTo end1
ReDim a(n)
avg1 = Int(m / n)'平均数
avg2 = m Mod n '余数
If n Mod 2 = 0 Then '偶数个拆分
For i = 1 To n / 2: a(n / 2 - i + 1) = avg1 - i: Next '按从小到大赋值前n/2
For i = 1 To n / 2: a(i + n / 2) = avg1 + i: Next '按从小到大赋值后n/2
Else '奇数个拆分
For i = 1 To Int(n / 2): a(Int(n / 2) - i + 1) = avg1 - i: Next
For i = 1 To Int(n / 2): a(i + Int(n / 2) + 1) = avg1 + i: Next
a(Int(n / 2) + 1) = avg1
End If
For i = n To (n - avg2 + 1) Step -1: a(i) = a(i) + 1: Next i '剩余数据从高到低补充
For i = 1 To n
Debug.Print i, a(i)
Next
end1:
End Sub