递归算法,程序开始计算后无响应
要生成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;
分不够可以再加。我的运行的函数如下:
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;
...全文
相关推荐
读懂C++递归程序
递归在计算学科中是一种非常重要的方法,计算理论中到处都有用递归进行表述的问题及求解方法。 在程序设计中,数据描述和算法表达也常用递归,通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算,大大地减少了程序的代码量。 掌握递归,计算机类专业的学生必须要做到。 本文基于一个C++递归程序,分步骤进行演示,帮助你知道其执行过程,试图帮助你啃下这块硬骨头。
程序无响应时的解决方法
最重要的是,保持耐心和冷静,一步步排查问题,最终找到并解决程序无响应的原因。确保你的代码在执行过程中没有长时间的阻塞操作。如果你的程序需要等待外部输入或输出,可以考考虑使用异步操作或多线程来提高程序的响应性。你可以使用pdb提供的命令来逐行执行代码、查看变量的值以及跟踪代码的执行流程。如果是这种情况,你可以尝试优化你的代码,减少资源的占用。在这种情况下,你可以尝试优化算法,使用更高效的数据结构或算法来改进程序的性能。异常处理:在你的代码中添加适当的异常处理机制,可以帮助你捕获和处理潜在的错误情况。
Java算法之递归算法计算阶乘
主要介绍了 Java递归算法计算阶乘,感兴趣的朋友可以参考下
阿克曼函数 c程序 递归与非递归算法的综合
简单易用的程序 包含了递归算法和栈模拟递归算法 两个程序
C++实现Fibonacci数列递归及非递归算法
C++实现Fibonacci数列递归及非递归算法


语言基础/算法/系统设计
