网易互娱笔试题_waiting&fighting的博客-程序员宅基地

技术标签: 笔试+面试  

顺时针和逆时针交替的蛇形矩阵

def matrix(n):
    # 生成n*n的方阵
    l = [[-1 for i in range(n)]for j in range(n)]
    # 设置边界条件
    right, left, down, up = True, False, False, False
    # 设置边界
    boundRight = n
    boundLeft = 0
    boundBottom = n
    boundTop = 0
    # 记录已经走过的格子数
    num = 0
    # 记录每次循环的坐标
    startofX = 0
    startofY = 0
    # 控制顺时针1还是逆时针-1
    direct = 1
    
    while num < n*n:
        # 从[0][0]开始向右走
        if direct == 1:
            # 顺时针
            if right:
                #print('向右走')
                for _ in range(startofY, boundRight):
                    num += 1
                    #print(startofX,_)
                    l[startofX][_] = num
                down = not down
                right = not right
                # 修改边界和起点
                boundTop += 1
                startofY = _
                startofX += 1
            # 到达右边界后向下走
            if down:
                #print('向下走')
                for _ in range(startofX, boundBottom):
                    num += 1
                    #print(_,startofY)
                    l[_][startofY] = num
                left = not left
                down = not down
                # 修改边界和起点
                boundRight -= 1
                startofX = _
                startofY -= 1
            # 到达下边界之后向左走
            if left:
                #print('向左走')
                for _ in range(startofY, boundLeft-1, -1):
                    num += 1
                    #print(startofX, _)
                    l[startofX][_] = num
                up = not up
                left = not left
                # 修改边界和起点
                boundBottom -= 1
                startofY = _
                startofX -= 1
            # 到达左边界后向上走
            if up:
                #print('向上走')
                for _ in range(startofX, boundTop-1, -1):
                    num += 1
                    #print(_,startofY)
                    l[_][startofY] = num
                right = not right
                up = not up
                # 修改边界和起点
                boundLeft += 1
                startofX = _
                startofY += 1
        else:
            # 逆时针
            # 先向下走
            if down:
                #print('向下走')
                for _ in range(startofX, boundBottom):
                    num += 1
                    #print(_,startofY)
                    l[_][startofY] = num
                right = not right
                down = not down
                # 修改边界和起点
                boundLeft += 1
                startofX = _
                startofY += 1
            # 到达下边界后向右走
            if right:
                #print('向右走')
                for _ in range(startofY, boundRight):
                    num += 1
                    #print(startofX,_)
                    l[startofX][_] = num
                up = not up
                right = not right
                # 修改边界和起点
                boundBottom -= 1
                startofY = _
                startofX -= 1
            # 到达右边界后向上走
            if up:
                #print('向上走')
                for _ in range(startofX, boundTop-1, -1):
                    num += 1
                    #print(_,startofY)
                    l[_][startofY] = num
                left = not left
                up = not up
                # 修改边界和起点
                boundRight -= 1
                startofX = _
                startofY -= 1
            # 到达上边界之后向左走
            if left:
                #print('向左走')
                for _ in range(startofY, boundLeft-1, -1):
                    num += 1
                    #print(startofX, _)
                    l[startofX][_] = num
                down = not down
                left = not left
                # 修改边界和起点
                boundTop += 1
                startofY = _
                startofX += 1
        
        if (boundTop >= 1 and right) or (boundLeft >= 1 and down):
            direct *= -1
            if direct == -1:
                # 设置边界条件
                right, left, down, up = False, False, True, False
            else:
                # 设置边界条件
                right, left, down, up = True, False, False, False
    '''
    for i in range(n):
        print('[',end='')
        #print(l[i])
        for j in range(n):
            print('{},'.format(l[i][j]), end='')
        print(']')
    '''
    return l
def test():
    C = int(input())
    # C组数据
    for i in range(C):
        N, M = map(int, input().split(' '))
        l = matrix(N)
        ret = []
        for j in range(M):
            row, col = map(int, input().split(' '))
            ret.append(l[row][col])
            #print(l[row][col])
        for k in ret:
            print(k)
        
test()

设计一个文件管理系统

new_fd = [i for i in range(1000000)]
used = {
    }

def openf(fname):
    for i in new_fd:
        if i not in used.keys():
            used[i] = fname
            return i
    print('All new_fd have been used!')
    return -1

def closefd(fd):
    if fd not in used.keys():
        print('close Error!')
    used.pop(fd)
        
def dup(fd):
    for i in new_fd:
        if i not in used.keys():
            used[i] = used[fd]
            return i
    print('All new_fd have been used!')
    return -1

def dup2(fd1, fd2):
    if fd1 not in used.keys():
        print('dup2 Error!')
    used[fd2] = used[fd1]
    

def operator(opt = '', opt_object = None):
    if opt == 'open':
        return openf(opt_object) 
    elif opt == 'close':
        closefd(opt_object)
        return ''
    elif opt == 'query':
        return used[opt_object]
    elif opt == 'dup':
        return dup(opt_object)
    elif opt == 'dup2':
        dup2(opt_object[0], opt_object[1])
        return ''
    else:
        print('No such operator!')
        return ''
        
def test():
    T = int(input())
    for epoch in range(T):
        N = int(input())
        ret = []
        used.clear()
        for optidx in range(N):
            s = list(input().split(' '))
            n = len(s)
            if n == 3:
                opt, fd1, fd2 = s[0], int(s[1]), int(s[2])
                opt_object = [fd1, fd2]
                retnow = operator(opt, opt_object)
                ret.append(retnow)
            else:
                opt, opt_object = s[0], s[1] 
                if opt == 'open':
                    retnow = operator(opt, opt_object)
                    ret.append(retnow)
                else:
                    retnow = operator(opt, int(opt_object))
                    ret.append(retnow)
        for result in ret:
            if result != '':
                print(result)
test()

求删掉一条边的情况下,使得神经网络Loss_cur-Loss_new最大

这题没写完,时间到了。。。

class node:
    def __init__(self, v, outputnode=None):
        self.v = v
        self.outputnode = outputnode
        self.inputnode = inputnode
    def output(self):
        ret = 0
        for node, w in self.inputnode:
            ret += w * node.v
        self.v = ret
        return ret

class net:
    def __init__(self, nodes, loss):
        self.nodes = nodes
        self.loss = loss
        
def test():
    # C轮输入
    C = int(input())
    for epoch in range(C):
        # N个样本,每个样本维度为D
        N, D = map(int, input().split(' '))
        X = []
        for i in range(N):
            s = list(map(int, input().split(' ')))
            x = list(s[:-1])
            X.append([x, s[-1]])
        # M个隐藏层,K条边
        M, K = map(int, input().split(' '))
        # Tlist存放每个隐藏层神经元的数量
        Tlist = list(map(int, input().split(' ')))
        # 新建nodes
        nodes = []
        # K行输入, 每行表示一条边
        for i in range(K):
            startnode, endnode, weight = input().split(' ')
            startnode_spl = startnode.split('_')
            if startnode_spl[0] == 'input':
                nodes.append(node(0))
                startid = startnode_spl[1]
            elif 
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_35090060/article/details/108837099

智能推荐

随便推点