在不受限内存情况猜数字次数最小的算法
来源:互联网 发布:广联达兴安得力软件 编辑:程序博客网 时间:2024/06/06 09:51
使用列出所有可能解集,然后收敛解集的方法 =================== Guess_Number_Lib.pas Unit Guess_Number_Lib; Interface Function Init : Integer; Implementation Var Function Init : Integer; Begin ASSIGN(PIN,FIN); RESET(PIN); for i:=1 to len do writeln(plog,'Game Started.'); End; Function Guess(Const g : array of integer) : Get_Back; Result.A := 0; for i:=1 to len do for i:=0 to 9 do for i:=1 to len do writeln(plog,'Ans : ',result.a,'A',result.b,'B'); End; Procedure OverGame; BEGIN =============== Main.pas Program Guess_Number; Type Var Procedure MD(x : Integer); Function Cmp(s1 , s2 : Tgue) : Get_Back; Result.a := 0; for i:=1 to len do End;
{$mode Delphi}
{$S+,Q+,N+}
Type
Get_Back = record
A , B : Integer;
End;
Function Guess(Const g : array of integer) :Get_Back;
Procedure OverGame;
Const
Fin = 'Guess.in';
Fout = 'Guess.out';
FLog = 'Guess.log';
// Flog = '';
Pin , Pout , PLog :Text;
Ans : array [0..10] ofInteger;
len : Integer;
Guess_Tot : Longint;
vis : array [0..9] ofBoolean;
Var
rand , i : Integer;
Randomize;
ASSIGN(PLOG,FLOG); REWRITE(PLOG);
fillchar(vis,sizeof(vis),False);
Guess_Tot := 0;
readln(pin,len);
CLOSE(PIN);
begin
repeat
rand := random(10);
until not vis[rand];
vis[rand] := True;
ans[i] := rand;
end;
Result := Len;
writeln(plog,'Code by Yizer');
for i:=1 to len do
if i<>len then
write(plog,ans[i],' ')
else
writeln(plog,ans[i]);
Var
i : Integer;
tmp : array [0..10] of Integer;
Begin
Inc(Guess_Tot);
writeln(plog,Guess_Tot,':');
write(plog,'You guess : ');
for i:=1 to len do
if i<>len then
write(plog,g[i],' ')
else
writeln(plog,g[i]);
Result.B := 0;
Fillchar(tmp,sizeof(tmp),0);
if (g[i]>-1) and(g[i]<10) then
inc(tmp[g[i]])
else
begin
writeln(plog,'Invialid parameters');
exit;
end;
if tmp[i]>1 then
begin
writeln(plog,'Invialidparameters');
exit;
end;
if g[i] = ans[i] then
inc(result.a)
else
if vis[g[i]] then
inc(result.b);
Begin
writeln(plog,'Game Over.');
writeln(plog,'Thank you for playing.');
ASSIGN(POUT,FOUT); REWRITE(POUT);
writeln(pout,Guess_Tot);
CLOSE(POUT);
CLOSE(PLOG);
End;
END.
{$mode delphi}
Uses
Guess_Number_Lib;
Tgue = array [0..10] of Integer;
len : Integer;
G : Tgue;
I : Integer;
ans , ans2 : Get_Back;
Flag : array [0..3628800] of Boolean;
f : array [0..3628800] of Tgue;
Tot : Longint;
visit : array [0..10] of Boolean;
rand : Longint;
Var
i : Longint;
Begin
if x = len+1 then
begin
inc(tot);
f[tot] := g;
end
else
for i:=0 to 9 do
if not visit[i] then
begin
g[x] := i;
visit[i] := True;
MD(x+1);
visit[i] := False;
end;
End;
Var
i : Integer;
Begin
Fillchar(visit,sizeof(visit),False);
for i:=1 to len do
visit[s1[i]] := True;
Result.b := 0;
if s1[i]=s2[i] then
inc(Result.a)
else
if visit[s2[i]] then
inc(Result.b);
BEGIN
Randomize;
len := Init;
MD(1);
Repeat
repeat
rand := random(tot) + 1;
until not flag[rand];
flag[rand] := True;
ans := Guess(f[rand]);
for i:=1 to tot do
if not flag[i] then
begin
ans2 := cmp(f[i],f[rand]);
if (ans2.a<>ans.a)or (ans2.b<>ans.b) then
flag[i] := True;
end;
Until ans.a = len;
Overgame;
END.
- 在不受限内存情况猜数字次数最小的算法
- 算法有序数组合并---在空间足够的情况下,进行O(n)的合并 并且移动次数最小
- 算法题目---数字在排序数组中出现的次数
- 算法-数字在排序数组中出现的次数
- 算法题/数字在排序数组中出现的次数
- 算法复习:数字在排序数组中出现的次数
- 一道面试题,内存受限的情况,如何在海量的数据中找到重复最多的
- 【算法】出现次数超过一半的数字
- 算法 求数字1出现的次数
- 数字在数组中出现的次数
- 数字在数组中出现的次数
- 编程算法 - 数字在排序数组中出现的次数 代码(C)
- 面试算法(三十八)数字在排序数组中出现的次数
- 算法--微软面试:指定数字在数组中出现的次数
- 如何在有限的内存的情况下,找到大量数据中重复查询次数最多的语句
- 在不知道算法原理的情况下,如何阅读理解算法的matlab程序
- [算法]找出旋转数组的最小数字
- 算法题目---旋转数组的最小数字
- 祝福!
- RNA与DNA
- struts2总结
- Microwindows:GUI窗口的IO控制机制
- DNA的分子编码基因
- 在不受限内存情况猜数字次数最小的算法
- 染色体的基因顺序遗传图谱
- 函数有界性
- 条件概率P(A|B)的逆概率-贝叶斯定理
- C#如何为winform程序打包发布应用
- 堆排序
- 扩展字符串匹配-BNDM算法扩展
- 这是我写的第一篇blog
- struts2