主题:求助,哪位朋友帮忙翻译一下。
哪位朋友能帮我翻译一下下面这个程序?
翻译为C或者C++,感激不尽,谢谢!!
program LV_Queens;
uses crt,dos;
const
M=10000;
var
x:array[1..M] of integer;
i:integer;
h1,m1,s1,hund1,
h2,m2,s2,hund2:word;
N: integer;
procedure PrintResult;
var
i,j: integer;
t1,t2:longint;
f:text;
ch: char;
begin
gettime(h2,m2,s2,hund2);
if (h2<h1) then h2:=h2+24;
t1:=1; t2:=1;
t1:=t1*s1*100+t1*m1*60*100+t1*h1*3600*100+hund1;
t2:=t2*s2*100+t2*m2*60*100+t2*h2*3600*100+hund2;
t1:=t2-t1;
hund1:=t1 mod 100; t1:=t1 div 100;
s1:=t1 mod 60; t1:=t1 div 60;
m1:=t1 mod 60; t1:=t1 div 60;
h1:=t1; writeln;
write('One answer found. Used time: ');
write(h1 div 10, h1 mod 10, ':');
write(m1 div 10, m1 mod 10, ':');
write(s1 div 10, s1 mod 10, '.');
writeln(hund1 div 10,hund1 mod 10);
write('Press any key to view result... ');
ch:=readkey; writeln;
for i:=1 to N do
begin
for j:=1 to N do
if j=x[i] then write ('Q':2)
else write('.':2) ;
writeln;
end;
writeln;
for i:=1 to N do write(x[i]:5);
writeln; writeln;
write('Save result to file (y/n)? ');
ch:=readkey; writeln(ch);
if upcase(ch)='Y' then
begin
assign(f,'QUEENS.TXT');
rewrite(f);
writeln(f,'One answer of ',N,' queens problem: ');
writeln(f);
for i:=1 to N do
begin
write(f,x[i]:5);
if i mod 16=0 then writeln(f);
end;
writeln(f);
close(f);
writeln('Result save to file: QUEENS.TXT');
end;
end;
function place(k,a:integer):boolean;
var
i:integer;
begin
for i:=1 to k-1 do
if (k-i)=abs(a-x[i]) then
begin
place:=false;
exit;
end;
place:=true;
end;
procedure queensLV;
var
i,j,k,c,t,stop: integer;
begin
for i:=1 to N do x[i]:=i;
k:=1; c:=1; stop:=1;
randomize;
while (k<=N) do
begin
c:=0;
for j:=k to N do
if place(k,x[j]) then
begin
c:=c+1;
if random(c)=0 then t:=j;
end;
if c>0 then
begin
if(t<>k) then
begin
x[k]:=x[k] xor x[t];
x[t]:=x[t] xor x[k];
x[k]:=x[k] xor x[t];
end;
k:=k+1;
end
else
begin
k:=k-stop;
stop:=stop+1;
if k<1 then
begin
k:=1;
stop:=1;
end;
end;
end;
end;
begin
clrscr;
writeln;
writeln('N Queens Problem.');
writeln('Programmed by j.t.Chang.');
writeln;
write('Enter N (N>=4): '); readln(N);
if (N<4) or (N>M) then
begin
writeln('Out of range: 4..',M);
halt;
end;
writeln('Finding one answer of ',N,' queens problem.');
writeln('Please wait...');
gettime(h1,m1,s1,hund1);
for i:=1 to N do x[i]:=0;
queensLV;
PrintResult;
end.
翻译为C或者C++,感激不尽,谢谢!!
program LV_Queens;
uses crt,dos;
const
M=10000;
var
x:array[1..M] of integer;
i:integer;
h1,m1,s1,hund1,
h2,m2,s2,hund2:word;
N: integer;
procedure PrintResult;
var
i,j: integer;
t1,t2:longint;
f:text;
ch: char;
begin
gettime(h2,m2,s2,hund2);
if (h2<h1) then h2:=h2+24;
t1:=1; t2:=1;
t1:=t1*s1*100+t1*m1*60*100+t1*h1*3600*100+hund1;
t2:=t2*s2*100+t2*m2*60*100+t2*h2*3600*100+hund2;
t1:=t2-t1;
hund1:=t1 mod 100; t1:=t1 div 100;
s1:=t1 mod 60; t1:=t1 div 60;
m1:=t1 mod 60; t1:=t1 div 60;
h1:=t1; writeln;
write('One answer found. Used time: ');
write(h1 div 10, h1 mod 10, ':');
write(m1 div 10, m1 mod 10, ':');
write(s1 div 10, s1 mod 10, '.');
writeln(hund1 div 10,hund1 mod 10);
write('Press any key to view result... ');
ch:=readkey; writeln;
for i:=1 to N do
begin
for j:=1 to N do
if j=x[i] then write ('Q':2)
else write('.':2) ;
writeln;
end;
writeln;
for i:=1 to N do write(x[i]:5);
writeln; writeln;
write('Save result to file (y/n)? ');
ch:=readkey; writeln(ch);
if upcase(ch)='Y' then
begin
assign(f,'QUEENS.TXT');
rewrite(f);
writeln(f,'One answer of ',N,' queens problem: ');
writeln(f);
for i:=1 to N do
begin
write(f,x[i]:5);
if i mod 16=0 then writeln(f);
end;
writeln(f);
close(f);
writeln('Result save to file: QUEENS.TXT');
end;
end;
function place(k,a:integer):boolean;
var
i:integer;
begin
for i:=1 to k-1 do
if (k-i)=abs(a-x[i]) then
begin
place:=false;
exit;
end;
place:=true;
end;
procedure queensLV;
var
i,j,k,c,t,stop: integer;
begin
for i:=1 to N do x[i]:=i;
k:=1; c:=1; stop:=1;
randomize;
while (k<=N) do
begin
c:=0;
for j:=k to N do
if place(k,x[j]) then
begin
c:=c+1;
if random(c)=0 then t:=j;
end;
if c>0 then
begin
if(t<>k) then
begin
x[k]:=x[k] xor x[t];
x[t]:=x[t] xor x[k];
x[k]:=x[k] xor x[t];
end;
k:=k+1;
end
else
begin
k:=k-stop;
stop:=stop+1;
if k<1 then
begin
k:=1;
stop:=1;
end;
end;
end;
end;
begin
clrscr;
writeln;
writeln('N Queens Problem.');
writeln('Programmed by j.t.Chang.');
writeln;
write('Enter N (N>=4): '); readln(N);
if (N<4) or (N>M) then
begin
writeln('Out of range: 4..',M);
halt;
end;
writeln('Finding one answer of ',N,' queens problem.');
writeln('Please wait...');
gettime(h1,m1,s1,hund1);
for i:=1 to N do x[i]:=0;
queensLV;
PrintResult;
end.