小红书2024算法实习-编程题,涂色

	题目描述:
    	给出一个数组,你需要求出按顺序对其进行一系列区间操作后最终所得的数组
    操作有如下三种:
        1.将下标在L到R之间的元素全部或上X
        2.将下标在L到R之间的元素全部与上X
        3.将下标在L到R之间的元素全部设为X
    输入描述:
        第一行有一个正整数N,代表数组的长度
        第二行有N个非负整数,代表数组中的元素
        第三行有一个正整数M,代表操作次数
        第四行有M个正整数,代表M次操作中的区间左端点L
        第五行有M个正整数,代表M次操作中的区间右端点R
        第六行是一个长度为M的字符串,'|'代表操作1,'&'代表操作2,'='代表操作3
        第七行有M个正整数,代表M次操作中的参数为X
    输入数据:
        4
        5 4 7 4
        4
        1 2 3 2
        4 3 4 2
        = | & =
        8 3 6 2
    输出数据:
        8 2 2 0
def one():
    N = int(input())
    return N
def two(N):
    arr = []
    index = 0
    while index < N:
        x = int(input())
        arr.append(x)
        index += 1
    return arr
def three():
    M = int(input())
    return M
def four(M):
    M_left = []
    index_m_left = 0
    while index_m_left < M:
        x = int(input())
        M_left.append(x)
        index_m_left += 1
    return M_left
def five(M):
    M_right = []
    index_m_right = 0
    while index_m_right < M:
        x = int(input())
        M_right.append(x)
        index_m_right += 1
    return M_right
def six(M):
    op = []
    index_m_op = 0
    while index_m_op < M:
        op_input = input()
        op.append(op_input)
        index_m_op += 1
    return op
def seven(M):
    arr_X = []
    index_m_X = 0
    while index_m_X < M:
        x = int(input())
        arr_X.append(x)
        index_m_X += 1
    return arr_X
def main():
    N = one()
    arr = two(N)
    M = three()
    M_left = four(M)
    M_right = five(M)
    op = six(M)
    arr_X = seven(M)
    i = 0
    while i < M:
        index_left = M_left[i] - 1
        index_right = M_right[i] - 1
        option = op[i]
        option_X = arr_X[i]
        if index_left == index_right:
            num = arr[index_left]
            if option == '=':
                num = option_X
                arr[index_left] = num
            elif option == '|':
                num = option_X | num
                arr[index_left] = num
            elif option == '&':
                num = option_X & num
                arr[index_left] = num
        else:
            for index in range(index_left, index_right+1):
                num = arr[index]
                if option == '=':
                    num = option_X
                    arr[index] = num
                elif option == '|':
                    num = option_X | num
                    arr[index] = num
                elif option == '&':
                    num = option_X & num
                    arr[index] = num
        i += 1
    print(arr)

if __name__ == '__main__':
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值