作者在 2015-02-22 22:34:10 发布以下内容
*一串不重复的数字,并且从小到大排列,如何比较快捷的判断出其中有哪几组数字是连续的,并且取出每组的两头值?
*如:1,3,6,8,9,10,12,15,20,23,24,25,26,30,33,34,35,36,37,38,39,40
*变成:1,3,6,8-10,12,15,20,23-26,30,33-40
Clear
lcStr="1,3,6,8,9,10,12,15,20,23,24,25,26,30,33,34,35,36,37,38,39,40"
nRow=Alines(Astr,Strtran(lcStr,",",Chr(13)+Chr(10)))
lcStr1=""
lnNum1=-1
For lnI=1 to nRow
lnNum=Int(Val(Astr[lnI]))-lnI
If lnNum1=lnNum
lcStr1=Iif(Not "-"$Right(lcStr1,Len(cStr)+1),lcStr1,Left(lcStr1,Len(lcStr1)-Len(cStr)-1))+"-"+Astr[lnI]
Else
lcStr1=lcStr1+Iif(Empty(lcStr1),"",",")+Astr[lnI]
EndIf
cStr=Astr[lnI]
lnNum1=lnNum
EndFor
MessageBox(lcStr1)
对上面方法做了些改进,运行速度有所提高 (2015-03-16)
Clear
lcStr="1,3,6,8,9,10,12,15,20,23,24,25,26,30,33,34,35,36,37,38,39,40"
nRow=Alines(Astr,Strtran(lcStr,",",Chr(13)+Chr(10)))
lcStr1=""
lnNum1=-1
cStr=""
For lnI=1 to nRow
lnNum=Int(Val(Astr[lnI]))-lnI
If lnNum=lnNum1
cStr=Astr[lnI]
Else
If Not Empty(cStr)
lcStr1=lcStr1+"-"+cStr
cStr=""
Endif
lcStr1=lcStr1+Iif(Empty(lcStr1),"",",")+Astr[lnI]
EndIf
lnNum1=lnNum
EndFor
If Not Empty(cStr)
lcStr1=lcStr1+"-"+cStr
cStr=""
EndIf
MessageBox(lcStr1)
方法三(2015-04-01)
Aa="1,3,6,8,9,10,12,15,20,23,24,25,26,30,33,34,35,36,37,38,39,40"
nRow=Alines(AcStr,Chrtran(Aa,",",Chr(13)+Chr(10)))
Store AcStr[1] To cStr,lcStr
For lnI=2 To nRow
If Val(AcStr[lnI])-Val(cStr)=1
lcStr=Iif("-"+cStr$lcStr,Strtran(lcStr,"-"+cStr,"-"+AcStr[lnI]),lcStr+"-"+AcStr[lnI])
Else
lcStr=lcStr+","+AcStr[lnI]
EndIf
cStr=AcStr[lnI]
EndFor
MessageBox(lcStr)
方法四 (2015-04-15)
Clear
lcStr="1,3,6,8,9,12,15,20,23,24,25,26,30,33,34,35,36,37,38,39,40"
lcStr=lcStr+",|"
nRow=Alines(Astr,Strtran(lcStr,",",Chr(13)+Chr(10)))
cStr1=Astr[1]
lcStr1=cStr1
lcStr2=""
For lnI=2 To nRow
If Val(Astr(lnI))=Val(cStr1)+1
lcStr2="-"+Astr(lnI)
Else
lcStr1=lcStr1+lcStr2+","+Astr(lnI)
lcStr2=""
EndIf
cStr1=Astr(lnI)
EndFor
MessageBox(left(lcStr1,len(lcStr1)-2),0,"显示结果")
方法五 (2015-04-06)
Clear
lcStr="1,3,6,8,9,12,15,20,23,24,25,26,30,33,34,35,36,37,38,39,40"
nRow=Alines(Astr,Strtran(lcStr,",",Chr(13)+Chr(10)))
dimension astr(alen(astr),1)
create cursor test (Num N(2))
insert into test from array astr
select min(Num) nmin,max(num) nmax,Num-recno() nid,iif(min(Num)=max(Num),transform(min(Num)),transform(min(Num))+"-"+transform(max(Num)))+space(10) from test group by nid into array Atemp
lcstr=Atemp[1,4]
for lnI=2 to alen(Atemp,1)
lcStr=lcStr-","-Atemp[lnI,4]
endfor
messagebox(alltrim(lcstr))