Home > other >  Blue cup mixed number python implementation
Blue cup mixed number python implementation

Time:03-24

On the Internet to see the great god of analytical contrast his code write again python version but the timeout is a great god can help to see where you can optimize
Blog links: https://blog.csdn.net/jopus/article/details/18998403

 # will be full array arr, divided into three sections a, b, c, judge whether meet the n=a + b/c 

# converts an array of interval Numbers
Def getNum (arr, x, y) :
Num=0
For I in range (x, y + 1) :
Num=num * 10 + arr [I]
Return num

# for all arrangement to deal with every permutation results
Def prim (arr, k, m) :
Global count
# prefix when was the last place, a kind of arrangement books
If k==m - 1:
# I said at the end of a location, not more than n digits
For I in range (length) :
Will be arr # 0 ~ I converted to an array of arrays, namely with the value of a
A=getNum (arr. Zero, I)
# locate b at the end of the
BLast=* (n - a) arr [8] % 10
# from a position at the end of the beginning, to the end of the array, the array of middle position in the future looking for a position at the end of the
bFor j in range (I + int ((8 - I)/2), 8) :

b # found at the end of the positionIf arr [j]==bLast:
# will be arr I + 1 ~ j is converted to an array of arrays, namely with the value of b
B=getNum (arr, I + 1, j)
# will be arr j + 1 ~ 8 is converted to an array of arrays, namely with the value of c
C=getNum (arr, j + 1, 8)
If b % c==0 and a + b/c==n:
Count +=1
Break
The else:
For I in range (k, m) :
# exchange prefix, and make it produce a prefix under
Arr [k], arr=arr [I] [I], arr [k]
Prim (arr, k + 1, m)
# prefix will change back, continue to do a prefix is arranged on the
Arr [k], arr=arr [I] [I], arr [k]


N=int (input ())
# define permutations array
Arr=[1, 2, 3, 4, 5, 6, 7, 8, 9]
# calculation input n several
Length=len (STR (n))
The count=0
Prim (arr, 0, 9)
Print (count)
  • Related