递归算法,程序开始计算后无响应
大地精灵 2011-07-16 10:29:36 要生成1296个含0,1的2进制数据,规律头尾相接任意取11位都跟另外的不重复,而且要求是1296个数据里,0和1的个数相等,我现在的电脑只要运算程序就变成没响应,不知道是否是递归太深的缘故,希望有人能够帮忙提出解决办法,如果能够提供更好的方法生成也可以,我采用的是递归操作。我的QQ:106873730,邮箱:ajq_8036@163.com.
分不够可以再加。我的运行的函数如下:
function TFrmMain.CharCount(s,c:string):Integer;
begin
Result := (Length(s) - Length(AnsiReplaceStr(s,c,''))) div (length(c));
end;
function TFrmMain.TestCode_IsPH(Test_Code:String):Boolean;
begin
if CharCount(Test_Code,'0') = CharCount(Test_Code,'1') then
Result := True
else
Result := False;
end;
function TFrmMain.TestByNum(TestCode,SourceCode:String;WS:Integer):Boolean;
var
Loop:Integer;
Flag :Boolean;
begin
Flag := True;
for Loop := 1 to length(SourceCode) - WS + 1 do
begin
if Copy(SourceCode,Loop,ws) = TestCode then
begin
Flag := False;
Break;
end;
end;
Result := Flag;
end;
//JZ--进制
//WS--位数
//TotalNum--个数
function TFrmMain.GetCodeByNum(MiddleCode:String;JZ,WS,TotalNum:Integer):String;
var
FirstCode:String;
CurrentCode:String;
Loop:Integer;
OverCode:String;
begin
self.StatusBar1.Panels[0].Text := IntToStr(StrToInt(self.StatusBar1.Panels[0].Text) + 1);
FirstCode := Copy(MiddleCode,1,Ws);
CurrentCode := Copy(MiddleCode,length(MiddleCode) - Ws + 2,Ws - 1);
if length(MiddleCode) = TotalNum + Ws - 1 then
begin
if (CurrentCode + Copy(MiddleCode,1,1) = FirstCode) then
begin
OverCode := copy(MiddleCode,1,length(MiddleCode) - WS + 1);
if TestCode_IsPH(OverCode) then
begin
GoalFlag := 1;
ListBox1.Items.Add(OverCode);
end;
end;
Exit;
end;
for Loop := 0 to Jz - 1 do
begin
if TestByNum(CurrentCode + IntToStr(Loop),MiddleCode,WS) then
begin
if GoalFlag <> 1 then
GetCodeByNum(MiddleCode + IntToStr(Loop),Jz,Ws,TotalNum);
end;
end;
end;
//调用
procedure TFrmMain.Button1Click(Sender: TObject);
begin
GetCodeByNum('00000000000',2,11,1296)
end;