~cpp class Turtle: def __init__(self, weight, strength): self.weight = weight self.strength = strength self.canAmount = strength - weight def inputATurtle(): numsStr = raw_input().split(' ') if(numsStr == ['']): return (0,0) weight = int(numsStr[0]) strength = int(numsStr[1]) return (weight, strength) def inputTurtles(turtles): while(True): (weight, strength) = inputATurtle() if(weight == 0 or strength == 0): break turtles.append(Turtle(weight, strength)) def turtlesCompare(t1, t2): result = 0 if(t1.canAmount > t2.canAmount): result = -1 elif(t1.canAmount < t2.canAmount): result = 1 return result def pileUpTurtles(turtles): totalWeight = turtles[0].weight canAmount = turtles[0].canAmount pile = [turtles[0]] for t in turtles[1:]: if(canAmount >= t.weight): canAmount -= t.weight if(canAmount > t.canAmount): canAmount = t.canAmount pile.append(t) #showTurtles(pile) return len(pile) def showTurtles(turtles): for t in turtles: print t.weight, t.strength, t.canAmount if __name__=='__main__': turtles = [] inputTurtles(turtles) turtles.sort(cmp = turtlesCompare) numOfTurtle = pileUpTurtles(turtles) print numOfTurtle
~cpp >>> class A: ... pass ... >>> a1=A();a1.foo=30 >>> a2=A();a2.foo=-1 >>> l=[a1,a2] >>> l.sort(key=lambda x:x.foo) >>> l[0].foo -1 >>> l.reverse() >>> l[0].foo 30 >>> import operator >>> l.sort(key=operator.attrgetter('foo')) >>> l[0].foo -1 >>> l.reverse() >>> l.sort(cmp=lambda a,b:cmp(a.foo,b.foo)) >>> l[0].foo -1 >>>
~cpp >>> class A: def __init__(self,foo): self.foo=foo def __cmp__(self,anA): return cmp(self.foo,anA.foo) def __repr__(self): return '<A : %d>'%self.foo >>> l=[A(i) for i in xrange(5,0,-1)] >>> l [<A : 5>, <A : 4>, <A : 3>, <A : 2>, <A : 1>] >>> l.sort() >>> l [<A : 1>, <A : 2>, <A : 3>, <A : 4>, <A : 5>]