SET TALK OFF
SET SAFETY OFF
CLEAR ALL
CLOSE DATABASES
CLEAR
*要求在100多个数值中快速挑选出最佳的数值合并成一个已固定数值。(不一定和固定值完一样只要尽可能相近就行,每个数只能选一次。)
TS=420000.00
USE TESTSHARED
SORT ON A1 /D TO L1
USE L1
ALTER TABLE L1 ADD A3 N(3)
REPLACE ALL A3 WITH RECNO()
SUM A1 TO T1
T10=0
SCAN
T10=T10+A1
IF T10>=TS
TR=RECNO()
EXIT
ENDIF
ENDSCAN
T11=T10-TS
DO Z01
RETURN
PROCEDURE Z01
REPLACE A3 WITH 1 FOR RECNO()<=TR
REPLACE A3 WITH 2 FOR A3<>1
SORT ON A3,A1 TO L2
USE L2
DO Z02 &&1对1替换
RETURN
PROCEDURE Z02 &&1对1替换
COPY TO L3 FOR A3=2 .AND. A1<>0
USE L3 IN 0
SCAN FOR A3=1
TA=A1
SELECT L3
LOCATE FOR TA-A1<T11 .AND. TA-A1>0
IF FOUND()
TB=A1
SELECT L2
REPLACE A3 WITH 2
LOCATE FOR A1=TB
REPLACE A3 WITH 1
DO Z03 &&整理1
ENDIF
SELECT L2
ENDSCAN
DO Z04 &&1对1替换完成
RETURN
PROCEDURE Z03 &&整理1
SELECT L3
USE
SELECT L2
SORT ON A3,A1 TO L4
ZAP
APPEND FROM L4
SUM A1 TO T10 FOR A3=1
T11=T10-TS
?T11
DO Z02 &&继续1对1替换
RETURN
PROCEDURE Z04 &&1对1替换完成
SELECT L3
COPY TO L5
SELECT 0
USE L5
DO Z05 &&1对2替换
RETURN
PROCEDURE Z05 &&1对2替换
SELECT L2
SCAN FOR A3=1
TA=A1
SELECT L3
SCAN
TB=A1
SELECT L5
LOCATE FOR TA-TB-A1<T11 .AND. TA-TB-A1>0
IF FOUND()
TC=A1
SELECT L2
REPLACE A3 WITH 2
LOCATE FOR A1=TB
REPLACE A3 WITH 1
LOCATE FOR A1=TC
REPLACE A3 WITH 1
DO Z06 &&整理2
ENDIF
SELECT L3
ENDSCAN
SELECT L2
ENDSCAN
DO Z07
RETURN
PROCEDURE Z06 &&整理2
CLOSE DATABASES
USE L2
SORT ON A3,A1 TO L4
ZAP
APPEND FROM L4
SUM A1 TO T10 FOR A3=1
T11=T10-TS
?T11
COPY TO L3 FOR A3=2
COPY TO L5 FOR A3=2
USE L3 IN 0
USE L5 IN 0
DO Z05 &&继续1对2替换
RETURN
SET TALK OFF
SET SAFETY OFF
CLEAR ALL
CLOSE DATABASES
CLEAR
*要求在100多个数值中快速挑选出最佳的数值合并成一个已固定数值。(不一定和固定值完一样只要尽可能相近就行,每个数只能选一次。)
TS=420000.00
USE TESTSHARED
SORT ON A1 /D TO L1
USE L1
ALTER TABLE L1 ADD A3 N(3)
REPLACE ALL A3 WITH RECNO()
SUM A1 TO T1
Sec1=Seco()
T10=0
SCAN
T10=T10+A1
IF T10>=TS
TR=RECNO()
EXIT
ENDIF
ENDSCAN
T11=T10-TS
DO Z01
RETURN
PROCEDURE Z01
REPLACE A3 WITH 1 FOR RECNO()<=TR
REPLACE A3 WITH 2 FOR A3<>1
SORT ON A3,A1 TO L2
USE L2
DO Z02 &&1对1替换
RETURN
PROCEDURE Z02 &&1对1替换
COPY TO L3 FOR A3=2 .AND. A1<>0
USE L3 IN 0
SCAN FOR A3=1
TA=A1
SELECT L3
LOCATE FOR TA-A1<T11 .AND. TA-A1>0
IF FOUND()
TB=A1
SELECT L2
REPLACE A3 WITH 2
LOCATE FOR A1=TB
REPLACE A3 WITH 1
DO Z03 &&整理1
ENDIF
SELECT L2
ENDSCAN
DO Z04 &&1对1替换完成
RETURN
PROCEDURE Z03 &&整理1
SELECT L3
USE
SELECT L2
SORT ON A3,A1 TO L4
ZAP
APPEND FROM L4
SUM A1 TO T10 FOR A3=1
T11=T10-TS
?T11
DO Z02 &&继续1对1替换
RETURN
PROCEDURE Z04 &&1对1替换完成
SELECT L3
COPY TO L5
SELECT 0
USE L5
DO Z05 &&1对2替换
RETURN
PROCEDURE Z05 &&1对2替换
SELECT L2
SCAN FOR A3=1
TA=A1
SELECT L3
SCAN
TB=A1
SELECT L5
LOCATE FOR TA-TB-A1<T11 .AND. TA-TB-A1>0
IF FOUND()
TC=A1
SELECT L2
REPLACE A3 WITH 2
LOCATE FOR A1=TB
REPLACE A3 WITH 1
LOCATE FOR A1=TC
REPLACE A3 WITH 1
DO Z06 &&整理2
ENDIF
SELECT L3
ENDSCAN
SELECT L2
ENDSCAN
DO Z07
RETURN
PROCEDURE Z06 &&整理2
CLOSE DATABASES
USE L2
SORT ON A3,A1 TO L4
ZAP
APPEND FROM L4
SUM A1 TO T10 FOR A3=1
T11=T10-TS
?T11
COPY TO L3 FOR A3=2
COPY TO L5 FOR A3=2
USE L3 IN 0
USE L5 IN 0
DO Z05 &&继续1对2替换
RETURN