2013-08-05 50 views
-1

我試圖做到以下幾點:高效的算法來劃分的時間間隔儘可能均等

中的幾個日期,也就是說,A,B和C,我想

  1. 分區(C - A)成N個間隔數
  2. 其中一個間隔必須有乙作爲其結合
  3. 的間隔應該儘可能接近相等儘可能

任何人都可以提出一個有效的算法來實現這個目標嗎?謝謝!

+1

是N給定還是計算? –

+0

將它們轉換爲毫秒併除以N? –

+0

嗨,N給出,一個固定的整數。 – haginile

回答

0

讓我們的例子:

A = 0 
B = 45 
C = 100 
N = 10 interval (10 interval = 11 bound) 

1:找到比X/N,其是最接近的比率AB/AC

4/10 < 45/100 5/10 
we will take X = 4 in this example (the result will vary depending on how you round it. 

2:設置從先前拍攝的結合數計算具有從A到B的綁定

A to B: 
    Interval number 4 (from previous value) 
    Bound number 5 
    Average interval length is (45-0)/4 = 11 
Bound 0 = 0 
Bound 1 = 11 
Bound 2 = 22 
Bound 3 = 33 
Bound 4 = 45 

3:設置從先前計算中獲得的綁定數以使boun d從B到C

B to C: 
    Interval number 6 (the rest) 
    Bound number 7 
    Average interval length is (100 - 45)/6 = 9 
Bound 4 = 45 
Bound 5 = 54 
Bound 6 = 63 
Bound 7 = 72 
Bound 8 = 81 
Bound 9 = 90 
Bound 10 = 100