free pascal方格填数(急~

问题描述:

free pascal方格填数(急~
下图中有4行方格,这10个格子中填入0~9这10个不同的数字,每行构成一个自然数,组成4个位数各不相同的自然数,已知这4个自然数都是某个整数的平方,求出所有的填写方案.一种可行的方案:9,81,324,7056.

【输出文件】 输出文件nmber.out文件包括若干行,每一行为一种可行的方案,包括四个整数,整数之间用一个空格分隔,最后一个数据后无空格.【输出样例】输出数据中的一行示例为:9 81 324 7056
1个回答 分类:综合 2014-12-02

问题解答:

我来补答
百度毁了我的格式...将就看吧,朴素算法..
var
use:array[0..9] of boolean;
i,j,k,l,a,b,c,d,e,o,p,q,r:longint;
begin
for i:=0 to 9 do //第一位的循环
begin
if round(sqrt(i))sqrt(i) then continue; //判断是不是平方
use[i]:=true; //表示它用过了
for j:=1 to 98 do //第二位的循环从01到98即可
begin
if (round(sqrt(j))sqrt(j)) then continue; //判断平方
a:=j div 10; //取十位
b:=j mod 10; //个位
if (a=b) or (use[a]) or (use[b]) then continue; //判断有没重的或者用过
use[a]:=true; //现在用过了
use[b]:=true; //同上
for k:=12 to 987 do //第三层012 到987
begin
if round(sqrt(k))sqrt(k) then continue; //楼主要会举一反三
c:=k div 100;
d:=k mod 100 div 10;
e:=k mod 10;
if (c=d) or (d=e) or (c=e) or (use[c]) or (use[d]) or (use[e]) then continue;
use[c]:=true;
use[d]:=true;
use[e]:=true;
for l:=123 to 9876 do
begin
if round(sqrt(l))sqrt(l) then continue;
\x09\x09o:=l div 1000;
\x09\x09p:=l mod 1000 div 100;
\x09\x09q:=l mod 100 div 10;
\x09\x09r:=l mod 10;
\x09\x09if (o=p) or (o=q) or (o=r) or (p=q) or (p=r) or (q=r) or
\x09\x09 (use[o]) or (use[p]) or (use[q]) or (use[r]) then continue;
\x09\x09writeln(i,' ',j,' ',k,' ',l); //都走到这一步了,直接输出就行了
\x09 end;
\x09 use[c]:=false; //用过的要还回来..待会还可能要用
\x09 use[d]:=false;
\x09 use[e]:=false;
end;
use[a]:=false;
use[b]:=false;
end;
use[i]:=false;
end;
readln;
end.
保证原创可实现 给分、~
 
 
展开全文阅读
剩余:2000