找回密码
 加入
搜索
查看: 3192|回复: 3

[AU3基础] 一组数字,找出最接近指定数字的数字

[复制链接]
发表于 2014-6-18 16:51:30 | 显示全部楼层 |阅读模式
原帖为http://www.autoitx.com/thread-43750-2-1.html
Local $vCZ = 55
Local $aNum[6] = [23, 34, 66, 80, 88, 95]
Local $rt = _GetApproachesNum($vCZ, $aNum)
MsgBox(0, @error, $rt)

Func _GetApproachesNum($vCZ, $aNum)
        If Not IsArray($aNum) Or UBound($aNum, 0) > 1 Then Return SetError(1, 0, 0)
        Local $i, $vC, $iMix, $vNeed
        For $i = 0 To UBound($aNum) - 1
                $vC = $aNum[$i] - $vCZ
                If $vC > 0 And (Not $iMix Or $vC < $iMix) Then Dim $vNeed = $aNum[$i], $iMix = $vC
        Next
        If Not $vNeed Then Return SetError(2, 0, 0)
        Return $vNeed
EndFunc   ;==>_GetApproachesNum
这里输出的是55以下最接近的数字66,但是现在有个新需求,就是想把以上最接近的数字34也输出来
因为a版给的函数就没看懂,不会自己修改,故来请教
发表于 2014-6-19 21:10:18 | 显示全部楼层
试试
Local $vCZ = 55
Local $aNum[6] = [23, 34, 66, 80, 88, 95]
Local $rt = _GetApproachesNum($vCZ, $aNum)
MsgBox(0, @error, $rt)

Func _GetApproachesNum($vCZ, $aNum)
        If Not IsArray($aNum) Or UBound($aNum, 0) > 1 Then Return SetError(1, 0, 0)
        Local $i, $vC, $iMix, $vC2, $iMix2, $vNeed, $vNeed2
        For $i = 0 To UBound($aNum) - 1
                $vC = $aNum[$i] - $vCZ
                $vC2 = $aNum[$i] - $vCZ
                If $vC2 < 0 And (Not $iMix2 Or $vC2 > $iMix2) Then Dim $vNeed2 = $aNum[$i], $iMix2 = $vC2
                If $vC > 0 And (Not $iMix Or $vC < $iMix) Then Dim $vNeed = $aNum[$i], $iMix = $vC
        Next
        If Not $vNeed Then Return SetError(2, 0, 0)
        Return $vNeed2 & ' > [' & $vCZ & '] < ' & $vNeed
EndFunc   ;==>_GetApproachesNum
发表于 2019-11-1 16:24:56 | 显示全部楼层
正好有查找最接近数字的需求,MARK一下。
发表于 2019-11-1 16:31:48 | 显示全部楼层
经过实测,函数有问题:
Global $fengli[72]
$fengli[0] = 71
$fengli[1] = 0
$fengli[2] = 0.1
$fengli[3] = 0.7
$fengli[4] = 1.1
$fengli[5] = 1.3
$fengli[6] = 1.8
$fengli[7] = 2
$fengli[8] = 2.3
$fengli[9] = 2.4
$fengli[10] = 2.8
$fengli[11] = 3
$fengli[12] = 3.5
$fengli[13] = 3.6
$fengli[14] = 4
$fengli[15] = 4.2
$fengli[16] = 4.6
$fengli[17] = 4.8
$fengli[18] = 5.2
$fengli[19] = 5.4
$fengli[20] = 5.8
$fengli[21] = 6
$fengli[22] = 6.4
$fengli[23] = 6.5
$fengli[24] = 7
$fengli[25] = 7.2
$fengli[26] = 7.5
$fengli[27] = 7.8
$fengli[28] = 8.2
$fengli[29] = 8.4
$fengli[30] = 8.8
$fengli[31] = 8.8
$fengli[32] = 9.3
$fengli[33] = 9.5
$fengli[34] = 9.8
$fengli[35] = 10
$fengli[36] = 10.6
$fengli[37] = 10.6
$fengli[38] = 11.2
$fengli[39] = 11.2
$fengli[40] = 11.7
$fengli[41] = 11.7
$fengli[42] = 12
$fengli[43] = 12.2
$fengli[44] = 12.7
$fengli[45] = 12.8
$fengli[46] = 13.2
$fengli[47] = 13.5
$fengli[48] = 13.8
$fengli[49] = 14
$fengli[50] = 14.2
$fengli[51] = 14.5
$fengli[52] = 14.8
$fengli[53] = 15
$fengli[54] = 15.5
$fengli[55] = 15.5
$fengli[56] = 16
$fengli[57] = 16
$fengli[58] = 16.5
$fengli[59] = 16.5
$fengli[60] = 17
$fengli[61] = 17
$fengli[62] = 17.5
$fengli[63] = 17.5
$fengli[64] = 18
$fengli[65] = 18
$fengli[66] = 18.5
$fengli[67] = 18.5
$fengli[68] = 19
$fengli[69] = 19
$fengli[70] = 19.5
$fengli[71] = 20

Local $input = 14



Local $rt = _GetApproachesNum($input, $fengli)
MsgBox(0, @error, $rt)

Func _GetApproachesNum($input, $fengli)
        If Not IsArray($fengli) Or UBound($fengli, 0) > 1 Then Return SetError(1, 0, 0)
        Local $i, $vC, $iMix, $vNeed
        For $i = 0 To UBound($fengli) - 1
                $vC = $fengli[$i] - $input
                If $vC > 0 And (Not $iMix Or $vC < $iMix) Then Dim $vNeed = $fengli[$i], $iMix = $vC
        Next
        If Not $vNeed Then Return SetError(2, 0, 0)
        Return $vNeed
EndFunc   ;==>_GetApproachesNum
返回值竟然是14.2,而不是14.
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-16 06:42 , Processed in 0.083731 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表