ABAP arithmetic problem: dislocation closest digital issue

A K-bit number N
(K≤2000, N≤1020)
to find the closest match and a large number N, with N the same as the sum of each number of the code to achieve purposes.
For example: request for the book Digital 0050 0104; 112 required number of 121;
algorithm analysis algorithm thought
direct violence, seeking the figure is not possible, the order is too large numbers, digital K-bit, can not be directly used int, or a float, expressed using storage arrays. This figure should be analyzed, step1, the minimum number of digits starting from the right start, break down the last digit, the decomposition of 1 to show it in front of one. 9 and 0 special, so start scanning from left to right, 0 skipped encountered, it encounters the first non-zero number, put this number -1, and then finally moved to the surface, and then, Step2, the first started looking for a non-numeric 9, if you encounter 9, 9 put into the final surface to encounter non-9, to +1, the end of the operation.
A-like example:
1999000--> 1990008-> 2,000,899
to pay attention to a problem that if this situation is 999000, at the beginning of the digits 1 up, the result is 1000899
a few unruly data: 29399--> 29489

The following is the code that should be a great space to simplify, but lazy to engage in

The DATA A the TYPE I.                                               " K≤2000 

the DATA : CNT the TYPE . I " an unlimited cumulative counter 
the DATA STR ( . 5 ) the TYPE . C
 " INPUT 
the DATA n- the TYPE P of VALUE ' 29 399 ' the LENGTH . 5 .                         " N≤1020 
the DATA K the TYPE I of VALUE . 5 .                                       " K≤2000 
" Output 
the DATA X the TYPE i.                                              "K≤2000

str = n.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING input  = str
  IMPORTING output = str.


DATA : off TYPE i, set TYPE i.

DATA:BEGIN OF wa_number,
       index  TYPE i,
       number TYPE i,
     END OF wa_number.
DATA it_number LIKE TABLE OF wa_number.

DO k TIMES.
  cnt = sy-index.
  off =  sy-index - 1.
  wa_number-index = sy-index.
  wa_number-number = str+off(1).
  APPEND wa_number TO it_number.
  CLEAR wa_number.
ENDDO.

SORT it_number  BY index DESCENDING.
cl_demo_output=>display( it_number ).

DO k TIMES.
  READ TABLE it_number INTO DATA(wa_tmp) INDEX sy-index.
  IF wa_tmp-number EQ 0.
  ELSE.
    IF sy-index eq 1.
      wa_tmp-number = wa_tmp-number - 1.
      MODIFY it_number FROM wa_tmp INDEX  sy-index.
    else.
      cnt += 1.
      wa_tmp-index = cnt.
      wa_tmp-number = wa_tmp-number - 1.
      MODIFY it_number FROM wa_tmp INDEX  sy-index.
    ENDIF.
    DATA(idx) =  sy-index + 1.
    PERFORM do_add USING idx.
    exit.
  ENDIF.
ENDDO.
*
CLEAR str..
SORT it_number  BY index.
LOOP AT it_number INTO wa_number..
  str = str && wa_number-number.
ENDLOOP.
cl_demo_output=>display( str ).

FORM do_add USING idx.

  SORT it_number  BY index DESCENDING.
  READ TABLE it_number INTO DATA(wa_tmp2) INDEX idx.
  IF sy-subrc EQ 0.
    IF wa_tmp2-number EQ 9.
      cnt = cnt + 1.
      DELETE it_number INDEX idx.
      wa_tmp2-index = cnt.
      APPEND wa_tmp2 TO it_number.
      DATA(idx2) = idx + 1.
      PERFORM do_add USING idx2.
    ELSE.
      wa_tmp2-number += 1.
      MODIFY it_number FROM wa_tmp2 INDEX idx.
    ENDIF.
  ELSE.
    wa_tmp-index = 0.
    wa_tmp-number = 1.
    APPEND wa_tmp2 TO it_number.
  ENDIF.

ENDFORM.

 

Guess you like

Origin www.cnblogs.com/yibing-jia/p/11280779.html