bilibili编程题

扭蛋机

def niudan(n):
    s=''
    n=int(n)
    while n:
        if n%2==0:
            n=(n-2)/2
            s+='3'
        else:
            n=(n-1)/2
            s+='2'
    print(s[::-1])
    return s
 
if __name__ == '__main__':
    n=input()
    niudan(n)

计算表达式

class Solution:
    def calculate(self,s):
        """
        :type s: str
        :rtype: int
        """
        def precedence(op):
            if op == '*' or op == '/':
                return 2
            else:
                return 1

        def cal(op, op1, op2):
            if op == '*':
                return op1 * op2
            elif op == '/':
                return op1 / float(op2)
            elif op == '+':
                return op1 + op2 
            else:
                return op1 - op2


        opstack = []
        operands = []

        # remove empty space and put operands and 
        idx = 0
        for i in range(idx, len(s)):
            if s[i] in '+-*/':
                operands.append(s[idx:i])
                while len(opstack) > 0 and precedence(s[i]) <= precedence(opstack[-1]) and len(operands) >= 2:
                    op = opstack.pop()
                    op2 = int(operands.pop())
                    op1 = int(operands.pop())
                    res = cal(op, op1, op2)
                    operands.append(res)
                opstack.append(s[i])
                idx = i + 1
        operands.append(s[idx:])

        while opstack:
             op = opstack.pop()
             op2 = int(operands.pop())
             op1 = int(operands.pop())
             res = cal(op, op1, op2)
             operands.append(res)

        return int(operands[0])    

            
            
if __name__ == '__main__':
    n=input()
    s=Solution()
    while n!='END' or n!='':
        if n=='END' or n=='':
            break
        print(s.calculate(n))
        n=input()

识别内网IP

def version(s):
    s=s.split(' ')
    s1=s[0].split('.')
    s2=s[1].split('.')
    print(s1,s2)
    for i in range(min(len(s1),len(s2))):
        
        if int(s1[i])==int(s2[i]):
            if i==min(len(s1),len(s2))-1:
                print('0')
            if s1[i+1] or s2[i+1]:
                continue
        else:
            if int(s1[i])>int(s2[i]):
                print('1')
                break
            else:
                print('-1')
                break
s=input()   
version(s) 

###顺时针打印矩阵


def printMatrix(matrix):
    # write code here
    walked = [[False] * (len(matrix[0])+1) for _ in range(len(matrix)+1)]
    for j in range(len(walked[-1])):
        walked[-1][j] = True
    for i in range(len(walked)):
        walked[i][-1] = True
    len_row = len(matrix) - 1
    len_col = len(matrix[0]) - 1
    res = []
    i = 0
    j = 0
    direction = 0  # 

    while not walked[i][j]:
        res.append(matrix[i][j])
        walked[i][j] = True
        if direction == 0: # right
            if j < len_col and not walked[i][j+1]:
                j += 1
            else:
                direction = 1
                i += 1
        elif direction == 1: # down
            if i < len_row and not walked[i+1][j]:
                i += 1
            else:
                direction = 2
                j -= 1
        elif direction == 2:  # left
            if j > 0 and not walked[i][j-1]:
                j -= 1
            else:
                direction = 3
                i -= 1
        elif direction == 3:  # up
            if i > 0 and not walked[i-1][j]:
                i -= 1
            else:
                direction = 0
                j += 1
    return res
n=input()
n=input()#获取第二行
m=[]
while n!='-1 -1':
    n=n.split(' ')
    m.append(n)
    n=input()
s=''
for i in printMatrix(m):
    s+=i
    s+=','
print(s[:-1]) 

是否存在和为k的两个数

def find(numbers,key):
#    record = []
    dirc = {}
    for i in range(len(numbers)): #用哈希表换来O(n^2)的时间复杂度
        if key-numbers[i] in dirc:
            dirc[key-numbers[i]] += [i]
        else:
            dirc[key-numbers[i]] = [i]
    for i in range(len(numbers)-1):
        for j in range(i+1,len(numbers)):
            if numbers[i] + numbers[j] in dirc:
                if i not in dirc[numbers[i] + numbers[j]] and j not in dirc[numbers[i] + numbers[j]]:
                    return True
num = input().split(",")
numbers = [int(i) for i in num[0].split()]
key = int(num[-1])
print(find(numbers, key))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值