首页 > 8.23爱奇艺笔试
头像
渣渣渣辉
编辑于 2020-08-23 21:49
+ 关注

8.23爱奇艺笔试

第一题,求n!中0的个数。
import sys
n = int(sys.stdin.readline().strip())
def func(n):
    count = 0
    while n>=5:
        count += n//5
        n //= 5
    return count
print(func(n))
第二题,判断路径是否相交。
给你一个字符串 path,其中 path[i] 的值可以是 'N'、'S'、'E' 或者 'W',分别表示向北、向南、向东、向西移动一个单位。

机器人从二维平面上的原点 (0, 0) 处开始出发,按 path 所指示的路径行走。

如果路径在任何位置上出现相交的情况,也就是走到之前已经走过的位置,请返回 True ;否则,返回 False 。
import sys
path = sys.stdin.readline().strip()
directions = {'N': (0, 1), 'S': (0, -1), 'E': (1, 0), 'W': (-1, 0)}
def func(path):
    temp = set()
    point = (0, 0)
    temp.add(point)
    for p in path:
        x, y = directions[p]
        point = (point[0] + x, point[1] + y)
        if point in temp:
            return True
        temp.add(point)
    return False
print(func(path))
第三题:判断一个输入的括号是否是有效括号
import sys
s = sys.stdin.readline().strip()
def func(s):
    dic=  {'}':'{', ']':'[', ')':'('}
    stack = []
    for c in s:
        if c not in dic:
            stack.append(c)
        else:
            if stack  and  stack.pop() != dic[c]:
                return False
    return not stack
print(func(s))



全部评论

(2) 回帖
加载中...
话题 回帖

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐