做为一名IT工作者,有些人觉得我不是班科出生,以前我也觉得如果是计算机系的可能要好些,但现在我觉得对于数学系(纯数学)的我,可能也有很大的优势,不在什么地方,就在算法实现上的不同,可能完全不是量级的变化,而是思考方式的变化,一个全排列的问题用矩阵去思想,却变得如此高效,真让我要好好反思,好好复习一些曾经学得很好的数分高代。
把n个数字, 放到 [][][]…[] * []…[], 令结果最大,乘号左边为n个数字中的若干个
还算高效的算法
def
calc(seq, where):
maximum, max_item = 0, []
for i in seq:
product = int(i[:where]) * int(i[where:])
if product > maximum:
maximum, max_item = product, i
elif product == maximum:
max_item += ' , ' + i
print " Maximum at " , max_item, " ,product " , maximum
def permute(seq):
l = len(seq)
if l <= 2 :
if l == 2 :
return [ seq, [seq[ 1 ], seq[0]] ]
else :
return [seq]
else :
res = []
for i in range(len(seq)):
rest = seq[:i] + seq[i + 1 :]
for x in permute(rest):
res.append(seq[i:i + 1 ] + x)
return res
import sys
seq = list(sys.argv[ 1 ])
where = int(sys.argv[ 2 ])
thelist = [ '' .join(x) for x in permute(seq) ]
print ' Got ' , len(thelist), ' items. '
calc(thelist, where)
maximum, max_item = 0, []
for i in seq:
product = int(i[:where]) * int(i[where:])
if product > maximum:
maximum, max_item = product, i
elif product == maximum:
max_item += ' , ' + i
print " Maximum at " , max_item, " ,product " , maximum
def permute(seq):
l = len(seq)
if l <= 2 :
if l == 2 :
return [ seq, [seq[ 1 ], seq[0]] ]
else :
return [seq]
else :
res = []
for i in range(len(seq)):
rest = seq[:i] + seq[i + 1 :]
for x in permute(rest):
res.append(seq[i:i + 1 ] + x)
return res
import sys
seq = list(sys.argv[ 1 ])
where = int(sys.argv[ 2 ])
thelist = [ '' .join(x) for x in permute(seq) ]
print ' Got ' , len(thelist), ' items. '
calc(thelist, where)
看看用矩阵的思维做的
def
solve(seq,where):
n = len(seq)
seq.sort()
seq.reverse()
table = [ [] for i in range(n) ]
left, right = where, n - where
leftr = long( ' 1 ' * left)
rightr = long( ' 1 ' * right)
flag = []
for item in [ int(x) for x in seq]:
for i in range(left):
table[left - i - 1 ] = (leftr + 10 ** i) * rightr
for i in range(right):
table[right - i + where - 1 ] = leftr * (rightr + 10 ** i)
for i in flag:
table[i] = 0
tablesorted = table[:]
tablesorted.sort()
maxindex = table.index(tablesorted[ - 1 ])
if maxindex >= where:
rightr = rightr + (item - 1 ) * 10 ** (right - maxindex + where - 1 )
else :
leftr = leftr + (item - 1 ) * 10 ** (left - maxindex - 1 )
flag.append(maxindex)
# print maxindex, leftr, rightr
return leftr, rightr
import sys
leftr, rightr = solve(list(sys.argv[ 1 ]),int(sys.argv[ 2 ]))
print " Maximum at " , leftr,rightr, ' ,product ' , leftr * rightr
n = len(seq)
seq.sort()
seq.reverse()
table = [ [] for i in range(n) ]
left, right = where, n - where
leftr = long( ' 1 ' * left)
rightr = long( ' 1 ' * right)
flag = []
for item in [ int(x) for x in seq]:
for i in range(left):
table[left - i - 1 ] = (leftr + 10 ** i) * rightr
for i in range(right):
table[right - i + where - 1 ] = leftr * (rightr + 10 ** i)
for i in flag:
table[i] = 0
tablesorted = table[:]
tablesorted.sort()
maxindex = table.index(tablesorted[ - 1 ])
if maxindex >= where:
rightr = rightr + (item - 1 ) * 10 ** (right - maxindex + where - 1 )
else :
leftr = leftr + (item - 1 ) * 10 ** (left - maxindex - 1 )
flag.append(maxindex)
# print maxindex, leftr, rightr
return leftr, rightr
import sys
leftr, rightr = solve(list(sys.argv[ 1 ]),int(sys.argv[ 2 ]))
print " Maximum at " , leftr,rightr, ' ,product ' , leftr * rightr