2016-12-25 62 views
0

您好如何劃分的間隔分成相等的部分 例如:如何劃分的間隔分成相等的部分

[1-100] divide by 5 part --> 

1- [1-20] 
2- [21-40] 
3- [41-60] 
4- [61-80] 
5- [81-100] 

[1-102] divide by 5 part --> 

1- [1-20] 
2- [21-40] 
3- [41-60] 
4- [61-80] 
5- [81-100] 
6- [100-102]* 

我嘗試一個代碼,但有時工作,並且在其他NUMS不因爲它必須工作 這是我做的(我知道我是數學周:P,我編碼它2周前,現在我不知道我是如何使它:D)


Func vall($a , $b) 
Local $inval = '' 
$all = $a 

$c = $b ; - 1 

$evv = Int($all/$c) 
$rrt = Int($all/$evv) 

$trtr = $evv 
$ee = 1 
$fg = 0 


if Mod($a,$evv) == 0 Then 

for $ll = 1 to $rrt ; $all 

    if $ll = $rrt Then 
     $inval = $inval & $ee&':-:'& $trtr 
    Else 
     $inval = $inval & $ee&':-:'& $trtr&',' 
    EndIf 


$ee = $ee + $evv 
$trtr = $trtr + $evv 
Next 

Else 

for $ll = 1 to $rrt ; $all 
$inval = $inval & $ee&':-:'& $trtr&',' 
$ee = $ee + $evv 
$trtr = $trtr + $evv 
Next 
$uu = $trtr -$evv + 1 
$inval = $inval & $uu&':-:'& $all 

EndIf 

Return $inval 

EndFunc 

我使用了AutoIt,但我需要的算法在任何郎使用它。

謝謝。

+0

確保從明年使用註釋每當你寫代碼的時候。這會幫助你練習。 –

回答

0

這是一個很容易理解的python實現。

def divide(number, parts): 
    '''number is the last number of the range and parts is no. of intervals you 
     want to make''' 
    chunksize = number//parts    # size of each interval 
    chunkstart = 1      # start of interval 
    chunkend = chunkstart + chunksize -1 # end of that interval 
    while chunkstart < number:   # don't go beyond the range 
     if chunkend > number:    # interval end is beyond the range 
      print chunkstart, number 
      break       # we are beyond the range now 
     print chunkstart, chunkend 
     chunkstart += chunksize   # take me to beginning of next interval 
     chunkend += chunksize    # also tell me where to end that 

Sample Input and Ouputs 

divide(100, 5) 
1 20 
21 40 
41 60 
61 80 
81 100 

divide(102, 5) 
1 20 
21 40 
41 60 
61 80 
81 100 
101 102 
+0

哇謝謝 我翻譯它爲自動和它的工作 – max

+0

但如果我使用57和30,它不工作 – max

+0

這是自動http://pastebin.com/4Cdu3VBY – max

0

@ pulkit-戈亞爾如u說,我做了同樣的分析[代碼幫助我理解它是如何工作後,所有的數學是甜蜜的:d] ,所以我得到的最好的方式得到正確的結果 是改變部分和測試部分和第二部分+ 1,直到它得到了最好的部分

我煮一個簡單的代碼:

$number = 57 
$parts = 30 
$yy= 0 
$yusf = 0 
ConsoleWrite ('number :' & $number& ' | parts : '&$parts& @CRLF) 
$partsplus = $parts 
$partsmoins = $parts 
if Mod($number,$parts) > Int($number/$parts) Then 

while $yusf = 0 

$partsplus += 1 
$partsmoins -= 1 
if Mod($number,$partsplus) < Int($number/$partsplus) Then 

ConsoleWrite ("old part : "&$parts & "| new part :" & $partsplus &" | interval : "&Int($number/$partsplus) &@CRLF) 
$parts = $partsplus 
$yusf = 1 

ElseIf Mod($number,$partsmoins) < Int($number/$partsmoins) Then 
    ConsoleWrite ("old part : "&$parts & "| new part :" & $partsmoins &" | interval : "&Int($number/$partsmoins ) & @CRLF) 
    $parts = $partsmoins 

    $yusf = 1 
    EndIf 
WEnd 

EndIf 
    $chunksize = Int($number/$parts)   ;; size of each interval 
    $chunkstart = 1      ;; start of interval 
    $chunkend = $chunkstart + $chunksize -1 ;; end of that interval 

    while $chunkstart <= $number   ;; don't go beyond the range 
     if $chunkend > $number Then   ;; interval end is beyond the range 
     ; print $chunkstart, $number 
     ConsoleWrite ("[ "&$chunkstart &" : "& $number &" ]*"& @CRLF) 
     ExitLoop 
     EndIf 
       ;; we are beyond the range now 
    ConsoleWrite ("[ "& $chunkstart &" : "& $chunkend &" ]"& @CRLF) 
    $yy += 1 
    $chunkstart += $chunksize   ;; take me to beginning of next interval 
    $chunkend += $chunksize    ;; also tell me where to end that 
    WEnd 

一些測試 57 - 30,30改爲28有很好的效果

number :57 | parts : 30 
old part : 30| new part :28 | interval : 2 
[ 1 : 2 ] 
[ 3 : 4 ] 
[ 5 : 6 ] 
[ 7 : 8 ] 
[ 9 : 10 ] 
[ 11 : 12 ] 
[ 13 : 14 ] 
[ 15 : 16 ] 
[ 17 : 18 ] 
[ 19 : 20 ] 
[ 21 : 22 ] 
[ 23 : 24 ] 
[ 25 : 26 ] 
[ 27 : 28 ] 
[ 29 : 30 ] 
[ 31 : 32 ] 
[ 33 : 34 ] 
[ 35 : 36 ] 
[ 37 : 38 ] 
[ 39 : 40 ] 
[ 41 : 42 ] 
[ 43 : 44 ] 
[ 45 : 46 ] 
[ 47 : 48 ] 
[ 49 : 50 ] 
[ 51 : 52 ] 
[ 53 : 54 ] 
[ 55 : 56 ] 
[ 57 : 57 ]* 

7 - 300,300改爲7:d

number :7 | parts : 300 
old part : 300| new part :7 | interval : 1 
[ 1 : 1 ] 
[ 2 : 2 ] 
[ 3 : 3 ] 
[ 4 : 4 ] 
[ 5 : 5 ] 
[ 6 : 6 ] 
[ 7 : 7 ] 

,在正常情況下 爲10-2,沒有變化

number :10 | parts : 2 
[ 1 : 5 ] 
[ 6 : 10 ]