楼主: pobel
1555 3

[SAS] WINDOW和DISPLAY语句实现汉诺塔 [推广有奖]

院士

14%

还不是VIP/贵宾

-

威望
2
论坛币
14673 个
通用积分
3462.8973
学术水平
932 点
热心指数
930 点
信用等级
730 点
经验
113854 点
帖子
1287
精华
4
在线时间
3645 小时
注册时间
2008-12-10
最后登录
2024-2-28

初级热心勋章 中级热心勋章 初级信用勋章 初级学术勋章 中级信用勋章 中级学术勋章 高级热心勋章 高级学术勋章

楼主
pobel 在职认证  发表于 2015-3-9 17:01:01 |只看作者 |坛友微信交流群|倒序 |AI写论文
相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
写了一个用来演示汉诺塔的宏,与各位分享:

1.宏包含两个参数
  n:用来指定盘子的个数;
   s_per_step: 用来设定每移动一步的时间,单位为秒


2.宏中省去了检验参数的步骤,n的值应该是正整数,s_per_step应该是一个正数,如0.1,0.5等。


3. 测试了n从1到15的执行情况,n变大时所需要的时间会加速度变长。一部分是宏构造SAS代码的时间,另一部分是移动盘子的时间。

4. 上传图片是顺序上传的,可显示确实倒过来的,不知道如何调整顺序了。
110.jpg

111.jpg
112.jpg
代码如下:


  1. %macro hanoi(
  2.      n=                /*specifies the number of disks*/
  3.     ,s_per_step=       /*specifies the time per step uses, unit: second*/
  4.     );

  5.     data _null_;
  6.        *** Set length for variables;
  7.            length %do i=1 %to &n;
  8.                      _1_&i _2_&i _3_&i  
  9.               %end; _temp _dash _null $%eval(2*&n+4)
  10.               _plate $%eval(6*&n+16)
  11.               message suminfo $100;

  12.            *** initialize variables;
  13.            array _tmp (%eval(3*&n)) _1_1--_3_&n;

  14.            do i=1 to %eval(3*&n);
  15.           _tmp(i)="|"||repeat(" ",(2*&n+1))||"|";
  16.            end;

  17.        ******************
  18.            Defines the Window
  19.            ******************;
  20.        window hanoi color=red rows=(23+&n) columns=(46+6*&n)
  21.                   #5   @(16+3*&n) "Tower of Hanoi" color=yellow protect=yes

  22.                   #8   @15 _dash color=yellow protect=yes
  23.                        @(21+2*&n) _dash color=yellow protect=yes
  24.                            @(27+4*&n) _dash color=yellow protect=yes

  25.                   #9   @15 _null color=yellow protect=yes
  26.                        @(21+2*&n) _null color=yellow protect=yes
  27.                            @(27+4*&n) _null color=yellow protect=yes

  28.               %do i=&n %to 1 %by -1;
  29.             #(9+&i) @15   _1_&i. color=yellow  protect=yes
  30.                                 @(21+2*&n) _2_&i color=yellow protect=yes
  31.                                 @(27+4*&n) _3_&i color=yellow protect=yes
  32.                               
  33.                   %end;

  34.                   #(10+&n)  @15 _dash color=yellow protect=yes
  35.                             @(21+2*&n) _dash color=yellow protect=yes
  36.                                     @(27+4*&n) _dash color=yellow protect=yes

  37.           #(11+&n)  @15 _plate color=cyan protect=yes

  38.           #(13+&n)  @15 suminfo color=cyan  protect=yes

  39.           #(14+&n)  @15 message color=cyan  protect=yes
  40.            ;

  41.            **********************************************
  42.            A macro for each of the steps to do the moving
  43.            **********************************************;
  44.            %macro step(N,From,Mid,To);
  45.                    %if &n=1 %then %do;
  46.                                 nth+1;
  47.                                         _temp= _&to._&&lastmiss&to;
  48.                                 _&to._&&lastmiss&to=_&from._%eval(&&lastmiss&from+1);
  49.                                         %let lastmiss&to=%eval(&&lastmiss&to-1);
  50.                                         _&from._%eval(&&lastmiss&from+1)=_temp;
  51.                                         %let lastmiss&from=%eval(&&lastmiss&from+1);

  52.                                 *** display the window to show the latest move;
  53.                                 display hanoi noinput;
  54.                                 link pause ;
  55.                    %end;
  56.                    %else %do;
  57.                       %step(%eval(&n-1),&from,&to,&mid);

  58.                                 nth+1;
  59.                                         _temp= _&to._&&lastmiss&to;
  60.                                 _&to._&&lastmiss&to=_&from._%eval(&&lastmiss&from+1);
  61.                                         %let lastmiss&to=%eval(&&lastmiss&to-1);
  62.                                         _&from._%eval(&&lastmiss&from+1)=_temp;
  63.                                         %let lastmiss&from=%eval(&&lastmiss&from+1);

  64.                                           *** display the window;
  65.                                 display hanoi noinput;
  66.                                 link pause ;

  67.                       %step(%eval(&n-1),&mid,&from,&to);
  68.                    %end;
  69.            %mend;      

  70.            ***************************
  71.            Prepare the initial figure.
  72.            ***************************;
  73.            _plate=repeat("_",%eval(6*&n)+16);
  74.            _dash=repeat("-",%eval(2*&n)+3);
  75.            _null="|"||repeat(" ",%eval(2*&n+1))||"|";

  76.            %do i=1 %to &n;
  77.                _1_&i.="|"||repeat(" ",%eval(&n-&i)) ||repeat("**",%eval(&i-1))||repeat(" ",%eval(&n-&i)) ||"|";
  78.            %end;

  79.            message="Press ENTER to start.";
  80.                   
  81.            *** Display the window and ask the user to press ENTER;
  82.            display hanoi blank;


  83.            *********************************
  84.            Display every step of the process
  85.            *********************************;
  86.            call missing(message,suminfo);
  87.            *** get the time when starts;
  88.            timest=time();
  89.            suminfo="Moving... Please wait...";

  90.            *** Initial value for some macro variables.
  91.                means the first rod is full and the other two are empty;
  92.            %let lastmiss1=0;
  93.            %let lastmiss2=&n;
  94.            %let lastmiss3=&n;

  95.            *** Move it;
  96.            %step(&n,1,2,3)

  97.            *** Get the time when it finishs;
  98.            timeen=time();

  99.            *** Prepare the summarize information;
  100.            totaltime=timeen-timest;

  101.            suminfo=cat("Done! Steps:"
  102.                        ,strip(put(nth,best.))
  103.                        ,"  Time:"
  104.                        ,strip(put(totaltime,8.1))
  105.                        ," seconds.");

  106.            message="Press ENTER to close.";

  107.            *** display the final status;
  108.            display hanoi blank;

  109.            *** Stop the execution;
  110.            stop;

  111.            *** Used to control how fast it moves;
  112.            pause:
  113.                  now=time() ;
  114.                  do while((now+&s_per_step)>time()) ;
  115.                  end ;

  116.     run;
  117. %mend;

  118. options nomprint;
  119. %hanoi(n=5,s_per_step=0.3)
  120. /*%hanoi(n=10,s_per_step=0.01)*/
复制代码




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Display Window wind disp play WINDOW DISPLAY 汉诺塔 sas

已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 100 + 100 + 5 + 5 + 5 精彩帖子
niuniuyiwan + 100 + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 200  论坛币 + 200  学术水平 + 10  热心指数 + 10  信用等级 + 10   查看全部评分

本帖被以下文库推荐

和谐拯救危机
沙发
niuniuyiwan 在职认证  发表于 2015-11-10 11:14:57 |只看作者 |坛友微信交流群
感谢分享 支持楼主
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

总评分: 经验 + 10  论坛币 + 3   查看全部评分

使用道具

藤椅
xddlovejiao1314 学生认证  发表于 2015-11-10 11:48:50 |只看作者 |坛友微信交流群
谢谢分享。

使用道具

板凳
wanlixing1208 发表于 2019-8-16 16:15:14 |只看作者 |坛友微信交流群
哇 竟然可以运用递归!! 太牛了 看的我一愣一愣的

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-25 09:18