level 1
小弟是pascal初学者,前几天在做noip2008提高组中的‘笨小猴’,题目很简单,但是我编代码时很快,但还只是得了50分,很郁闷,检查了半小时都检查不出什么来,后来我的和标准答案对比,发觉只有在判断质数时候有些不同。我把答案中判断质数的代码换上我的,发现竟然得了100分,让我倍感郁闷,下面我发两段判断质数的代码,望各位大虾能指导一下问题所在。
我的:
function check(n:longint):boolean;{n是要判断的数是否质数}
var
ab:boolean;
i:longint;
begin
ab:=true;
if n>2 then
begin
for i:=2 to round(sqrt(n)) do
if n mod i=0 then begin ab:=false; break; end;
end;
check:=ab;
end;
标准答案:
function check(n:longint):boolean;
var
i:longint;
begin
if n<2 then begin check:=false;exit;end;
for i:=2 to n-1 do
if n mod i=0 then begin check:=false;exit;end;
check:=true;
end;
2010年08月02日 00点08分
1
我的:
function check(n:longint):boolean;{n是要判断的数是否质数}
var
ab:boolean;
i:longint;
begin
ab:=true;
if n>2 then
begin
for i:=2 to round(sqrt(n)) do
if n mod i=0 then begin ab:=false; break; end;
end;
check:=ab;
end;
标准答案:
function check(n:longint):boolean;
var
i:longint;
begin
if n<2 then begin check:=false;exit;end;
for i:=2 to n-1 do
if n mod i=0 then begin check:=false;exit;end;
check:=true;
end;