回 帖 发 新 帖 刷新版面

主题:填数问题

从整数1到10中任取九个不同的数,填入下列九个格子中,
使所有相邻的(左、右、上、下)三个格子中的数的和都是素数。请用程序找出所有答案。
    ---------------------             
    |     |      |      |
    ---------------------       
    |     |      |      |
    ---------------------                       
    |     |      |      |         例如:
    ---------------------
    
                                      ---------------------                                       |  1  |  2  |  5  |
                                       -------------------  
                                       |  4  |  3  |  8  |             
                                       -------------------
                                       |  7  |  10 |  9  |
                                       -------------------

回复列表 (共4个回复)

沙发

这条题很简单,我会做,但现在没时间,有时间再来。

板凳

请楼上写出来

3 楼

HELP

4 楼

var a:array[1..3,1..3]of integer;
    t:integer;
function prim(x:integer):boolean;
  var can:boolean;
      i:integer;
  begin
    can:=true;
    for i:=2 to trunc(sqrt(x)) do begin
      if x mod i=0 then begin
         can:=false;break;
      end;
    end;
    prim:=can;
  end;
procedure dg(x,y:integer);
  var i,j,h,l:integer;
      can:boolean;
  begin
    if x=4 then begin
       inc(t);
       writeln(t,':');
       for i:=1 to 3 do begin
         for j:=1 to 3 do write(a[i,j]:4);
         writeln;
       end;
    end else begin
      for i:=1 to 10 do begin
        can:=true;
        for j:=1 to (x-1)*3+y-1 do begin
          h:=(j-1)div 3+1;
          l:=(j-1)mod 3+1;
          if a[h,l]=i then begin
             can:=false;break;
          end;
        end;
        if can then begin
           if ((y=1)or(prim(a[x,y-1]+i)))and((x=1)or(prim(a[x-1,y]+i))) then begin
              a[x,y]:=i;
              if y=3 then dg(x+1,1)
              else dg(x,y+1);
           end;
        end;
      end;
    end;
  end;
begin
  t:=0;
  dg(1,1);
end
(希望有分加)^_^

我来回复

您尚未登录,请登录后再回复。点此登录或注册