好未来的几道题,python

1. x + y = x | y

T = int(raw_input())
for _ in range(T):
    read = raw_input().split(' ')
    x, k = int(read[0]), int(read[1])
    digit = [0 for i in range(70)]
    for i in range(0, 70):
        if (x & (2 ** i)) == (2 ** i):
            digit[i] = 1

    # print(digit)
    zero = [0 for i in range(70)]
    for i in range(0, 70):
        if i > 0:
            zero[i] = zero[i - 1]
        if digit[i] == 0:
            zero[i] += 1

    ans = [0 for i in range(70)]
    for i in range(69, -1, -1):
        if digit[i] == 1:
            ans[i] = 0
        else:
            left = zero[i] - 1
            if k >= (2 ** left):
                ans[i] = 1
                k -= (2 ** left)
    ret = 0
    for i in range(70):
        if ans[i] == 1:
            ret += 2 ** i
    print(ret)

2. 0~10排列

# from itertools import permutations
show = raw_input().split(' ')
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
must, left = [], []
for i in range(10):
    if show[i] == '1':
        must.append(i)
    else:
        left.append(i)

ans = []
extra = []

def calc(choose):
    global ans
    ans.append(sorted(choose))

calc(must)

def dfs(pos, have, goal):
    if have == goal:
        choose = [i for i in must]
        for i in extra:
            choose.append(i)
        calc(choose)
        return
    if pos == len(left):
        return

    dfs(pos + 1, have, goal)
    extra.append(left[pos])
    dfs(pos + 1, have + 1, goal)
    extra.pop(-1)

for i in range(1, len(left) + 1):
    dfs(0, 0, i)

ret = []
for i in ans:
    s = []
    for j in range(len(i)):
        s.append(str(i[j]))
    ret.append("".join(s))
ans = sorted(ret)
for i in ans:
    print(i)

3.\ 期望

read = raw_input().split(' ')
n, m = int(read[0]), int(read[1])
data = raw_input().split(' ')
sum = 0
for i in range(n):
    sum += int(data[i])
print("%.2f" % (1.0 * sum / (n - m)))
#好未来##题解##笔试题目##Python#
全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
04-02 21:36
点赞 评论 收藏
转发
2 10 评论
分享
牛客网
牛客企业服务