码迷,mamicode.com
首页 > 其他好文 > 详细

每次迈1步或者两步,走完10级台阶的组合。

时间:2020-09-03 16:40:30      阅读:43      评论:0      收藏:0      [点我收藏+]

标签:text   clear   排序   tar   time   index   line   desc   算法   

*&---------------------------------------------------------------------*
*& Report  zabc
*& 循环+栈
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zabc.

TYPES BEGIN OF ty_num.
TYPES  num TYPE i.
TYPES END OF ty_num.

DATA gt_num TYPE TABLE OF ty_num.
DATA gt_stack TYPE TABLE OF ty_num.

DATA gv_target_num TYPE VALUE 10.

PERFORM frm_initialize."可以任意数字组合

SORT gt_num BY num."算法优化,务必排序。

PERFORM lookup.
*&---------------------------------------------------------------------*
*&      Form  frm_initialize
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_initialize.
  DATA ls_num TYPE ty_num.

  DO TIMES.
    ls_num-num = sy-index.
    APPEND ls_num TO gt_num.
  ENDDO.
ENDFORM.                    "frm_initialize
*&---------------------------------------------------------------------*
*&      Form  lookup
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->SY-TABIX   text
*----------------------------------------------------------------------*
FORM lookup.
  DATA ls_num TYPE ty_num.

  LOOP AT gt_num INTO ls_num.
    PERFORM push USING ls_num.

    DATA lv_num TYPE i.

    PERFORM get_count USING lv_num."取组合中数字的累计值

    IF lv_num = gv_target_num.
      PERFORM write.
    ENDIF.

    IF lv_num > gv_target_num ."组合值大于目标值,不用再递归,因为值越加越大

    ELSE.
      PERFORM lookup.
    ENDIF.

    PERFORM popup.

  ENDLOOP.
ENDFORM.                    "recurs

*&---------------------------------------------------------------------*
*&      Form  push
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PS_NUM     text
*----------------------------------------------------------------------*
FORM push USING ps_num.
  APPEND ps_num TO gt_stack.
ENDFORM.                    "push
*&---------------------------------------------------------------------*
*&      Form  write
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM write.
  FIELD-SYMBOLS <ls_num> TYPE ty_num.

  LOOP AT gt_stack ASSIGNING <ls_num>.
    AT FIRST.
      WRITE: / <ls_num>-num.
      CONTINUE.
    ENDAT.

    WRITE <ls_num>-num.
  ENDLOOP.
ENDFORM.                    "write

*&---------------------------------------------------------------------*
*&      Form  popup
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM popup.
  DATA lv_lines TYPE i.

  DESCRIBE TABLE gt_stack LINES lv_lines.

  DELETE gt_stack INDEX lv_lines.
ENDFORM.                    "popup
*&---------------------------------------------------------------------*
*&      Form  GET_COUNT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_count USING pv_num.
  FIELD-SYMBOLS <ls_num> TYPE ty_num.

  CLEAR pv_num..

  LOOP AT gt_stack ASSIGNING <ls_num>.
    pv_num = pv_num + <ls_num>-num.
  ENDLOOP.

ENDFORM.                    " GET_COUNT

每次迈1步或者两步,走完10级台阶的组合。

标签:text   clear   排序   tar   time   index   line   desc   算法   

原文地址:https://www.cnblogs.com/sap-George/p/13553209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!