关于质数判断的问题
pascal吧
全部回复
仅看楼主
level 1
liushifei2549 楼主
    小弟是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
level 1
liushifei2549 楼主
噢,我自己弄明白了
2010年08月02日 00点08分 2
level 5
function check(n:longint):boolean;{n是要判断的数是否质数}
var
    i,x:longint;
begin
    check:=true;
    if n=2 then exit(true);
    if n mod 2=0 then exit(false);
    i:=3;
    x:=trunc(sqrt(n));
    while i<=n do
    begin
        if (n mod x = 0 ) then exit(false);
        inc(i,2);
    end;
end;
楼上低调……

2010年08月04日 10点08分 5
level 2
我笨小猴AC的程序:
program bxh;
var
   a:array[1..26]of integer;
   s:string;
   i,maxn,minn,x:integer;
function ss(n:word):boolean;
   var
     i:integer;
   begin
     ss:=true;
     for i:=2 to trunc(sqrt(n)) do
       if n mod i=0 then begin ss:=false;exit; end;
     if (n=0)or(n=1) then ss:=false;
   end;
begin
   minn:=100;
   readln(s);
   for i:=1 to length(s) do
     case s[i] of
     'a'..'z':inc(a[ord(s[i])-96]);
     end;
   for i:=1 to 25 do
     if a[i]<>0 then
       begin
         if a[i]>maxn then maxn:=a[i];
         if a[i]<minn then minn:=a[i];
       end;
   x:=maxn-minn;
   if ss(x)=true then
     begin
       writeln('Lucky Word');
       writeln(x);
     end
   else begin
       writeln('No Answer');
       writeln('0');
     end;
   readln;
end.
2010年08月06日 04点08分 7
level 1
你的程序第三行a后面要有一个逗号。
2016年05月22日 12点05分 10
1