如何将间隔分成相等的部分

时间:2016-12-25 02:08:17

标签: algorithm math autoit

您好如何将间隔分成相等的部分 例如:

[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]*

我尝试使用代码,但有时会工作,而其他工作则无法正常工作 这就是我所做的(我知道我是数学周: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,但我需要算法在任何语言中使用它。

谢谢。

2 个答案:

答案 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

答案 1 :(得分:0)

@ pulkit-goyal你说我做了同样的分析[你的代码帮助我理解它在所有数学运算之后是如何工作的:D] 所以我得到了获得正确结果的最佳方法 是改变零件,测试零件1和零件+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 ]