设万维读者为首页 广告服务 技术服务 联系我们 关于万维
简体 繁体 手机版
分类广告
版主:诤友
万维读者网 > 教育学术 > 帖子
Benren2:小学一年级算术题蛮力破解程序
送交者: Benren2 2017年04月01日22:27:04 于 [教育学术] 发送悄悄话

笨人 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)



0%(0)
0%(0)
  python code - xi11west 04/03/17 (1049)
  python  /无内容 - xi11west 04/03/17 (1026)
  算术题不用代数也不用电脑 - kshdjj 04/02/17 (1099)
标 题 (必选项):
内 容 (选填项):
实用资讯
回国机票$360起 | 商务舱省$200 | 全球最佳航空公司出炉:海航获五星
海外华人福利!在线看陈建斌《三叉戟》热血归回 豪情筑梦 高清免费看 无地区限制
一周点击热帖 更多>>
一周回复热帖
历史上的今天:回复热帖
2016: 计算机
2016: 法律是维护社会稳定,而非帮你赚钱
2015: D.QU著:现有xx学科科学原理不等同于"绝
2015: 中军:人生究竟是什么
2013: 关于中医和科学的随想(2)
2013: 我坚持上海要把外地外国人赶出去,上海
2012: 请看看UBC教授的胡说八道和污蔑中国人
2012: Rabbit:暗物质与暗能量的暗地思考