4 the result of investment

数组funds,公司ABC初市值为a b c,每次对当前市值最低公司投资funds的数值,返回最后ABC
最基本的方式是每次都进行比较,找最小值然后加funds(i),但我的程序竟然跑不起来,而且我现在还找不到原因,放上来打自己脸:

class Solution: 
    def getAns(self, funds, a, b, c):
        # Write your code here
         A = a
         B = b 
         C = c
        for i in funds:
            
            if A<=B&A<=C:
                A+=i
            elif B<=C&B<A:
                B+=i 
            elif C<A&C<B:
                C+=i
        return A,B,C
           

然后是脑子转过来的修改后版本

class Solution: 
    def getAns(self, funds, a, b, c):
        # Write your code here
        A = a
        B = b 
        C = c
        for i in funds:
            
            if A<=B and A<=C:
                A = A + i
            elif B<=C and B<A:
                B = B + i 
            elif C<A and C<B:
                C = C + i
            
        return A,B,C

哈哈哈哈我竟然在循环赋值
然后python没有‘&’。。。
其实最开始实现是想用稍微不那么低级的min函数,结果一直想对min函数的运算结果赋值…当然没有结果了
不过也是因为有盲点,一是不知道对dict的操作可以通过key和value在items中进行分别比较,二是多参数的for循环还没理解好,这里是最简单的多变量for循环

dic = {'A':1,'B':2,'C':3}
for i,j in dic.items():
	#print(i)
	print(j)

而且思维僵化,if value = min(),dict[key] += i就是正确的对min函数结果操作方式了

猜你喜欢

转载自blog.csdn.net/qq_33612402/article/details/85251711