网易互娱笔试8.11:三道题AC

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_18310041/article/details/99209562

第一题:

给n个数组,计算每个数组里面的元素有多少个1,相同1次数为一类,计算每个数组有几类。

思路:减1做&运算。注意将答案放在数组里面分别打印,否则报错。

#coding=utf-8
# 本题为考试多行输入输出规范示例,无需提交,不计分。
import sys
def count_one(nums):
    res = []
    for num in nums:
        count = 0
        if num < 0:
            num = num & 0xffffffff
        while num:
            num = (num - 1) & num
            count += 1
        res.append(count)
    return len(set(res))

if __name__ == "__main__":
    # 读取第一行的n
    g = int(sys.stdin.readline().strip())
    ans = []
    # 例子个数
    for i in range(g):
        n = int(sys.stdin.readline().strip())
        line = sys.stdin.readline().strip()
        line = list(map(int, line.split()))
        ans.append(count_one(line))
    for i in ans:
        print(i)

第二题:

游泳池,一个进水管和一个排水管,分别每度过 t1, t2 时间改变开关的状态,分别每分钟排入 m1,m2 的水量。计算 t 时刻泳池的水量。

思路:分别计算进水管和排水管每分钟入水的水量,有个技巧如下,直接每分钟增加 t1 长度的数组,最后得到的数组长度为(t1 * t)。排水管则取排除水量的负数即可。

for i in range(1, t+1):
    if in_pip == -1:
        in_w += [0] * t1
        in_pip = - in_pip
    else:
        in_w += [m1] * t1
        in_pip = - in_pip

遍历总时间,每分钟计算进水量和出水量的和,如果小于0,则取0;如果大于m,则取m。

#coding=utf-8
# 本题为考试多行输入输出规范示例,无需提交,不计分。
import sys

def count_water(nums):
    m = nums[0]
    t = nums[1]
    m1 = nums[2]
    t1 = nums[3]
    m2 = nums[4]
    t2 = nums[5]

    in_w = []
    out_w = []
    all_water = 0
    in_pip = 1
    out_pip = 1
    in_t = 0
    out_t = 0

    for i in range(1, t+1):
        if in_pip == -1:
            in_w += [0] * t1
            in_pip = - in_pip
        else:
            in_w += [m1] * t1
            in_pip = - in_pip



    for i in range(1, t+1):
        if out_pip == -1:
            out_w += [0] * t2
            out_pip = - out_pip
        else:
            out_w += [-m2] * t2
            out_pip = - out_pip

    for i in range(t):
        all_water = max(0, in_w[i] + out_w[i] + all_water)
        if all_water > m:
            all_water = m
    return all_water

if __name__ == "__main__":
    # 读取第一行的n
    n = int(sys.stdin.readline().strip())
    ans = []
    for i in range(n):
        # 读取每一行
        line = sys.stdin.readline().strip()
        # 把每一行的数字分隔后转化成int列表
        values = list(map(int, line.split()))
        ans.append(count_water(values))
    for i in ans:
        print(i)

第三题:

给定n个字符串,判断字符串最大连续N的个数(可以容许改变两个字符)

思路:滑动窗口,右边界遍历所有的元素,如果滑动窗口里面的“非N字符个数”大于了2,则左边界右移动直到等于2。

#coding=utf-8
# 本题为考试多行输入输出规范示例,无需提交,不计分。
import sys
def count(line):
    # 左边界
    l = 0
    count_n = 0
    res = 0
    for r, char in enumerate(line):

        if char == 'N':
            count_n += 1
        # 最多2个改变
        while r - l + 1 - count_n > 2:
            if line[l] == 'N':
                count_n -= 1
            l += 1
        res = max(res, r-l+1)
    return res

if __name__ == "__main__":
    # 读取第一行的n
    n = int(sys.stdin.readline().strip())
    ans = []
    for i in range(n):
        # 读取每一行
        v = str(sys.stdin.readline().strip())
        ans.append(count(v))
    for i in ans:
        print(i)

猜你喜欢

转载自blog.csdn.net/qq_18310041/article/details/99209562