这个问题好不好解决?

daixiulin 2008-07-04 11:58:21
数组中有123,186,890,159,878,999,,,
规定0和9是相连的,
1、求不相连的号,如186,025;
2、求2相连的号,如124,895,907;
3、求3相连的号,如012,890
设计一个函数如:
function leiInStrs(str1:string;strs:TStrings):boolean;
var
lei:String;
begin
if ((strtoint(str1[1]) mod 3)=(strtoint(str1[2]) mod 3)) and ((strtoint(str1[2]) mod 3)=(strtoint(str1[3]) mod 3)) then lei:='不连';
if ((strtoint(str1[1]) mod 3)=(strtoint(str1[2]) mod 3)) and ((strtoint(str1[2]) mod 3)=(strtoint(str1[3]) mod 3)) then lei:='二连';
if ((strtoint(str1[1]) mod 3)=(strtoint(str1[2]) mod 3)) and ((strtoint(str1[2]) mod 3)=(strtoint(str1[3]) mod 3)) then lei:='三连';

Result:=strs.IndexOf(lei)>=0;
end;
...全文
62 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kampan 2008-07-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gzmhero 的回复:]
上面函数 StrLeiCnt没考虑9和0的问题,改下:

Delphi(Pascal) code

function StrLeiCnt(str:string):integer;
var
iCnt:integer;
begin
iCnt:=Ord(StrToInt(str[2])-StrToInt(str[1])=1)
+Ord(StrToInt(str[3])-StrToInt(str[2])=1)
+Ord((str[1]='9') and (str[2]='0'))
+Ord((str[2]='9') and (str[3]='0'));

if iCnt<>0 then
inc(iCnt);
Res…
[/Quote]

厉害,简单明了!我也明白了楼主的意思了。
gzmhero 2008-07-04
  • 打赏
  • 举报
回复

procedure LeiStrsCustom(strIn:TStrings;strOut:TStrings;iType:integer);//0:不连,2:2号连,3:3号连
var
i:integer;
begin
strOut.Clear;
for i:=0 to strIn.Count-1 do
begin
if StrLeiCnt(strIn.Strings[i])=iType then
strOut.Add(strIn.Strings[i]);
end;
end;
//使用

LeiStrsCustom(strs,Memo1.Lines,3);

gzmhero 2008-07-04
  • 打赏
  • 举报
回复
上面函数 StrLeiCnt没考虑9和0的问题,改下:


function StrLeiCnt(str:string):integer;
var
iCnt:integer;
begin
iCnt:=Ord(StrToInt(str[2])-StrToInt(str[1])=1)
+Ord(StrToInt(str[3])-StrToInt(str[2])=1)
+Ord((str[1]='9') and (str[2]='0'))
+Ord((str[2]='9') and (str[3]='0'));

if iCnt<>0 then
inc(iCnt);
Result:=iCnt;
end;

gzmhero 2008-07-04
  • 打赏
  • 举报
回复


function StrLeiCnt(str:string):integer;
var
iCnt:integer;
begin
iCnt:=Ord(StrToInt(str[2])-StrToInt(str[1])=1)+Ord(StrToInt(str[3])-StrToInt(str[2])=1);
if iCnt<>0 then
inc(iCnt);
Result:=iCnt;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
i:integer;
strs:TStringList;
str2:string;
begin
strs:=TStringList.Create;
strs.Add('128');
strs.Add('296');
strs.Add('123');
for i:=0 to strs.Count-1 do
Memo1.Lines.Add(strs.Strings[i]+','+IntToStr(StrLeiCnt(strs.Strings[i]))+'连号');
strs.Free;
end;

kampan 2008-07-04
  • 打赏
  • 举报
回复
这是啥子意思,是问啊,还是答啊?
jacknes009 2008-07-04
  • 打赏
  • 举报
回复
引用 3 楼 gzmhero 的回复:
上面函数 StrLeiCnt没考虑9和0的问题,改下:

Delphi(Pascal) code

function StrLeiCnt(str:string):integer;
var
iCnt:integer;
begin
iCnt:=Ord(StrToInt(str[2])-StrToInt(str[1])=1)
+Ord(StrToInt(str[3])-StrToInt(str[2])=1)
+Ord((str[1]='9') and (str[2]='0'))
+Ord((str[2]='9') and (str[3]='0'));

if iCnt <>0 then
inc(iCnt);
Res…


厉害,简单明了!我也明白了楼主的意思了。

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧