Groundhog and 2-Power Representation

Groundhog and 2-Power Representation

牛客多校A

python + 栈乱搞

ans=0
c=list(input())
n=int(len(c))
c.insert(0,'s')
k=[]
ans=[]
tot=int(0)

def ksm(a,b):
    r=int(1)
    while b>0:
        if(b%2==1):
            r=r*a
        a=a*a
        b=b//2
    return r

for i in range(1,n+1):
    if c[i]=='2':
        ans.append(int(2))
    if c[i]=='0':
        ans.append(int(0))
    if c[i]=='(':
        k.append('^')
    if c[i]=='+':
        k.append('+')
    if c[i]==')':
        while len(k)>0:
            t=k[-1]
            k.pop()
            if t=='+':
                sum=int(0)
                sum=sum+ans[-1]
                ans.pop()
                sum=sum+ans[-1]
                ans.pop()
                ans.append(sum)
            if t=='^':
                temp1=ans[-1]
                ans.pop()
                temp2=ans[-1]
                ans.pop()
                ans.append(ksm(temp2,temp1))
                break

while len(ans)>0:
    tot=tot+ans[-1]
    ans.pop()
print(tot)
原文地址:https://www.cnblogs.com/war1111/p/13457489.html