正の整数配列を指定し、すべての要素を組み合わせて最大の値(答え)を探します.

914 ワード

 
  
class MaxNumber(object):
    def __init__(self, intlst):
        self.lst = intlst
        self.ilen = len(self.lst)
        self.result = []
        self.max_number = 0

    def addlst(self, intlist):
        clst = [str(i) for i in intlist]
        return int("".join(clst))

    def presult(self):
        for i in self.result: print i
        print self.max_number

    def f(self, offset):
        if offset == ilen-1:
            if self.addlst(lst) > self.max_number:
                self.max_number = self.addlst(lst)
            self.result.append(self.lst[:])
            return
        for i in xrange(offset, self.ilen):
            lst[i], lst[offset] = lst[offset], lst[i]
            self.f(offset + 1)
            lst[i], lst[offset] = lst[offset], lst[i]

lst = [9, 3, 32, 46, 50]
imax = MaxNumber(lst)
imax.f(0)
imax.presult()