开发环境
elphi6+SQL Server2000这个问题是个组合问题,依次是从7个数字里取1到7个数字的组合,我以取4个数字为例,C(7,4)=35种.建表: CREATE TABLE [CBList] ( [YF] [char] (5) COLLATE Chinese_PRC_CI_AS NOT NULL , [Number] [char] (20) COLLATE Chinese_PRC_CI_AS NOT NULL , CONSTRAINT [PK_CBList] PRIMARY KEY CLUSTERED ( [YF], [Number] ) ON [PRIMARY] ) ON [PRIMARY]GO根据楼主给出的网址,我以'02 05 07 11 13 17 19'这个7个数字进行查询,符合满足4个数字的结果有10组,将这10组数字放到表里:(符合3个数字的,我只拿了两条) YF Number 07002 01,02,06,11,14,19,27 07007 02,03,11,15,19,24,28 07023 05,07,16,17,19,20,24 07053 02,05,07,12,13,25,28 07112 02,11,13,17,20,27,28 07117 02,07,09,11,17,20,21 07120 02,05,07,11,22,23,26 08007 02,05,10,11,17,18,20 08044 02,05,07,15,17,18,26 08105 02,11,13,19,23,24,26 08129 06,07,11,13,17,18,24 09089 01,02,06,08,13,17,19程序要根据组合的结果生成SQL语句:...implementation{$R *.dfm}//M1:TMemovar a:array[0..100] of integer; n:integer=7; r:integer=4;//以4个数字为例procedure HS(t: integer;AList:TStrings);//用回溯法进行组合var i:Integer; s:string;begin if t>r then begin s:=inttostr(a[1]); for i:=2 to r do s:=s+','+IntToStr(a
); AList.Add(s); end else begin for i:=1 to n do begin a[t]:=i; if (a[t-1]<a[t]) then HS(t+1,AList); end; end;end;procedure TForm1.Button1Click(Sender: TObject);var sList,TmpList:TStringList; i,j:integer; a7:array[1..7] of string;begin sList:=TStringList.Create; try HS(1,sList); //以02 05 07 11 13 17 19这组数字为例 a7[1]:='02'; a7[2]:='05'; a7[3]:='07'; a7[4]:='11'; a7[5]:='13'; a7[6]:='17'; a7[7]:='19'; TmpList:=TStringList.Create; try for i:=0 to sList.Count-1 do begin TmpList.Delimiter:=','; TmpList.DelimitedText:=sList.Strings; M1.Lines.Add('Select * From CBList where 1=1 and '); M1.Lines.Add('('); for j:=0 to r-2 do M1.Lines.Add(' charindex('''+a7[StrToInt(TmpList.Strings[j])]+''',Number)<>0 and '); M1.Lines.Add(' charindex('''+a7[StrToInt(TmpList.Strings[r-1])]+''',Number)<>0 '); M1.Lines.Add(')'); M1.Lines.Add('UNION'); end; M1.Lines.Delete(M1.Lines.Count-1);//删除最后的UNION finally TmpList.Free; end; finally sList.Free; end;end;将Memo里生成的SQL查询语句拷贝到SQL Server查询分析器里执行,可以得出结果。注: 符合满足4个数字的结果,同样也会符合满足3个数字,所以在处理3个数字的时候要剔除掉符合4个数字的结果,以此类推。