笨人 20170402
在微信同步贴上《小学一年级算术题求解》后,有人问能否给个破解程序。我说没问题。我用SAS和Python编程得到相同结果,但SAS比Python快很多。那就给个SAS的吧。
该小学一年级的算术题说的是:8个变量a, b, c, d, e, f, g, h, 在1到8范围内取不同值, 求解下列4个等式中此8个变量的赋值:
1. a+b=9
2. c+d=7
3. e-f=1
4. g-h=3
下面是用SAS软件编的蛮力破解此题的程序:
%macro DoValuation(Eq4);
data Output_&Eq4 (Keep=Variable Valuation label="Variable Valuation where g-h=&Eq4");
label Variable='Variable'
Valuation='Valuation';
length Variable $1 Valuation s1-s8 $7;
array varArr{8} a b c d e f g h;
array lblArr{8} $1 ('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h');
array strArr{8} $7 s1-s8;
do a=1 to 4;
do b=8 to 5 by -1;
do c=1 to 3;
do d=6 to 4 by -1;
do e=2 to 8;
do f=1 to 7;
do g=4 to 8;
do h=1 to (8 - &Eq4);
AnySame=0;
do i=1 to 7;
do j=i+1 to 8;
if varArr(i)=varArr{j} then
AnySame=1;
end;
end;
if AnySame=0 and a+b=9 and c+d=7 and
e-f=1 and g-h=&Eq4 then do i=1 to 8;
if strArr(i) ne '' then strArr(i)=cat(strip(strArr(i)),
put(varArr(i),1.));
else strArr(i)=put(varArr(i),1.);
end;
end;
end;
end;
end;
end;
end;
end;
end;
if s1 ne '' then do i=1 to 8;
Variable=lblArr(i);
Valuation=strip(strArr(i));
output;
end;
run;
%mend DoValuation;
%DoValuation(3)