Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. def split_strings(mas, ans, row, row_ans, col_ans):
  2.     result = []
  3.     prevIdx = []
  4.     isSplited = False
  5.     for index, string in enumerate(mas):
  6.         if len(string) > 1:
  7.             for i in range(1, len(string)):
  8.                 num1 = int(string[:i])
  9.                 num2 = int(string[i:])
  10.                 if num1 - num2 != 0:
  11.                     tmp = abs(num1 - num2)
  12.                     result.append(str(tmp))
  13.                     prevIdx.append(index)
  14.                     if tmp < ans:
  15.                         row_ans = row
  16.                         col_ans = len(result) - 1
  17.                         ans = tmp
  18.                     isSplited = True
  19.     return result, prevIdx, ans, row_ans, col_ans, isSplited
  20.  
  21.  
  22. t = int(input())
  23. for i in range(t):
  24.     s = input()
  25.     mas = [[int(s), -1]]
  26.     tmpSplit = [s]
  27.     tmpSplitIndexes = []
  28.     row_ans = 0
  29.     col_ans = 0
  30.     row = 0
  31.     ans = int(s)
  32.     if len(s) == 1:
  33.         print(1, s)
  34.         continue
  35.     while True:
  36.         row += 1
  37.         tmpSplit, tmpSplitIndexes, ans, row_ans, col_ans, isSplited = split_strings(tmpSplit, ans, row, row_ans, col_ans)
  38.         if isSplited:
  39.             tmp = []
  40.             for j in range(len(tmpSplit)):
  41.                 tmp.append([int(tmpSplit[j]), tmpSplitIndexes[j]])
  42.             mas.append(tmp)
  43.         else:
  44.             break
  45.     r = row_ans - 1
  46.     path = [ans]
  47.     idx = mas[row_ans][col_ans][1]
  48.     while r > 0:
  49.         path.append(mas[r][idx][0])
  50.         idx = mas[r][idx][1]
  51.         r -= 1
  52.     path.append(mas[0][0])
  53.     print(row_ans + 1, *path[::-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement