這次noip看到旁邊用c++那位對拍得好爽,
自己也搜了一下對拍的方法,
可惜多不適合pascal黨,
所以我中和幾種方法,終於弄出了一個用 命令提示符對拍的方法。
首先要有一個 百分百對的程序
我寫了個排序
a.pas
var
n,e:longint;
a:array[0..1000]of longint;
procedure init;
var
i:longint;
begin
read(n);
for i:=1 to n do read(a[i]);
end;
procedure main;
var
i,j:longint;
begin
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j]
then
begin
a[0]:=a[i];
a[i]:=a[j];
a[j]:=a[0];
end;
end;
procedure print;
var
i:longint;
begin
for i:=1 to n do write(a[i],' ');
end;
begin
assign(input,'in.txt');
assign(output,'o1.txt');
reset(input);
rewrite(output);
init;
main;
print;
close(input);
close(output);
end.
還要有一個待檢驗的程序,快拍(經檢驗是正確的,呵呵)
b.pas
var
n,e:longint;
a:array[0..1000]of longint;
procedure
init;
var
i:longint;
begin
read(n);
for i:=1 to n do read(a[i]);
end;
procedure
qsort(x,y:longint);
var
i,j,k:longint;
begin
if x>=y then exit;
i:=x;
j:=y;
k:=a[(x+y)div 2];
while i<=j do
begin
while a[i]<k do
inc(i);
while a[j]>k do
dec(j);
if i<=j
then
begin
a[0]:=a[i];
a[i]:=a[j];
a[j]:=a[0];
inc(i);
dec(j);
end;
end;
qsort(x,j);
qsort(i,y);
end;
procedure print;
var
i:longint;
begin
for i:=1 to n do write(a[i],' ');
end;
begin
assign(input,'in.txt');
assign(output,'o2.txt');
reset(input);
rewrite(output);
init;
qsort(1,n);
print;
close(input);
close(output);
end.
還要有一個 數據生成器
maker.pas
var
n,i:longint;
begin
assign(output,'in.txt');
rewrite(output);
randomize;
n:=random(1000)+1;
writeln(n);
for i:=1 to n do
writeln(random(maxlongint)+1);
close(output);
end.
最核心的部分就是這裏
新建一個文本文檔,後綴名改爲“.bat”
在裏面打
:loop
maker.exe
a.exe
b.exe
fc o1.txt o2.txt
if errorlevel 1 goto end
goto loop
:end
dos語言:loop//循環
maker.exe//運行 數據生成器
a.exe//運行 正確程序
b.exe//運行 待檢測程序
fc o1.txt o2.txt//比較 兩個程序的輸出結果
if errorlevel 1 goto end//如果 有錯誤(兩個輸出文件不一樣) 就跳到end
goto loop//跳到loop
:end
或者還可以這樣寫
:loop
maker.exe>in.txt
a.exeo1.txt
b.exeo2.txt
fc o1.txt o2.txt
if errorlevel 1 goto end
goto loop
:end
要是這樣寫,a b maker 三個程序中都不能些文件讀入/輸出
:loop
maker.exe>in.txt//運行 數據生成器 把結果輸出到 in.txt
a.exeo1.txt//運行 正確程序 讀入爲 in.txt 輸出爲 o1.txt
b.exeo2.txt//運行 待檢測程序 讀入爲 in.txt 輸出爲 o2.txt
fc o1.txt o2.txt
if errorlevel 1 goto end
goto loop
:end
注意:
1.程序檢測的是執行程序,而不是源程序。所以每改一次源程序都要編譯一次。
2.dos中貌似不識別 .in .out 文件,所以要用 .txt