Bilibili written test programming questions

1. Black tea

 

Title description

高贵的蕾米莉亚大小姐每天需要饮用定量 B 型血的红茶以保持威严,并且要分两杯在不同时段饮用。
女仆长十六夜咲夜每天可以制作很多杯不同剂量 B 型血的红茶供蕾米莉亚大小姐饮用。
某日,你和天才妖精琪露诺偷偷潜入红魔馆被咲夜抓住,要求在今日份的红茶中挑出所有满足大小姐要求的茶杯,否则……

Enter description:

每个样例有三行输入,第一行输入表示茶杯个数,第二行输入表示每份茶杯里的 B 型血剂量,第三行表示大小姐今天的定量

Output description:

对每一个样例,输出所有可能的搭配方案,如果有多种方案,请按每个方案的第一杯 B 型血剂量的大小升序排列。
如果无法找到任何一种满足大小姐的方案,输出"NO"(不包括引号)并换行。

Analysis: Since it is sorted according to the size of the first cup of blood dose, it is enough to sort all blood doses from small to large. Traverse from the beginning and output the appropriate matching scheme. The time complexity and space complexity are both O(n/2), and the coding is as follows:

n = int(input())
arr = list(map(int,input().split()))
sum = int(input())

arr.sort()

flag = 0
low = 0
high = len(arr)-1
while low<high :
    if arr[low]+arr[high]==sum :
        print(str(arr[low])+' '+str(arr[high]))
        low += 1
        flag = 1
    elif arr[low]+arr[high]>sum :
        high -=1
    else :
        low +=1

if flag==0:
    print('NO')

 

2. Determine whether the IP string belongs to the intranet IP

Title description

从业 666 年的 BILIBILI 网络安全工程师 KindMo 最近很困惑,公司有一个业务总是受到 SSRF 攻击。请帮他写一个程序,判断输入的字符串是否属于内网IP,用于防御该漏洞。
我们知道常见的内网IP有,127.0.0.1,192.168.0.1 等。

Enter description:

每次输入仅包含一个IP字符串,即一个测试样例

Output description:

对于每个测试实例输出整数1或0,1代表True,即输入属于内网IP,0代表False,即输入不属于内网IP或不是IP字符串。

Analysis: Cut the input string, and then judge whether the string is within the IP range of the intranet. The following code submitted on Niuke.com can also pass, it should be a system bug. .

iplist = input().split('.')
res = 0

if iplist[0] in ['127','10','192','172']:
    res = 1
    
print(res)

 

Three, flip the linked list

 

Title description:

对于一个链表 L: L0→L1→…→Ln-1→Ln,
将其翻转成 L0→Ln→L1→Ln-1→L2→Ln-2→…

输入是一串数字,请将其转换成单链表格式之后,再进行操作

Enter description:

一串数字,用逗号分隔

Output description:

一串数字,用逗号分隔

Analysis: You can set a list to store the result data. After the input data is converted into a list, it is traversed, and the fetching and storing of the list elements are judged according to the position. The encoding is as follows:

instr = input().strip().split(',')
res = []

for i in range(1,len(instr)+1):
    if i%2==1 :
        res.append(instr[int(i/2)])
    else :
        res.append(instr[len(instr)-int(i/2)])

print(','.join(res))

 

Fourth, version comparison

Title description

如果version1 > version2 返回1,如果 version1 < version2 返回-1,不然返回0.

输入的version字符串非空,只包含数字和字符.。.字符不代表通常意义上的小数点,只是用来区分数字序列。例如字符串2.5并不代表二点五,只是代表版本是第一级版本号是2,第二级版本号是5.

Enter description:

两个字符串,用空格分割。
每个字符串为一个version字符串,非空,只包含数字和字符.

Output description:

只能输出1, -1,或0

Analysis: Convert the input into two int type arrays, then compare bit by bit, and return the corresponding results according to the corresponding rules. Two variables, flag and res, are defined here to record the result flag and result value respectively. When flag=1, the result has come out, and the loop is out of the loop. It should be noted that the value of the loop and the condition for jumping out of the loop. Here I have taken the maximum length of the list in the loop range. In addition to the flag variable, the jumping out condition also judges the loop variable. When the loop variable = the shortest list length, It means that one of the lists has been matched, and it exactly matches a part of the other list. The result is not clear. If these two points are ignored, it is very likely that the array will be out of bounds. The encoding is as follows:

indata = input().split()
v1, v2, flag,res = list(map(int,indata[0].split('.'))) ,list(map(int,indata[1].split('.'))) , 0,0

for i in range(max(len(v1),len(v2))):
    if flag == 1 or i == min(len(v1),len(v2)):
        break
    if v1[i] > v2[i]:
        res = 1
        flag = 1
    elif v1[i] < v2[i]:
        res = -1
        flag = 1
    else:
        continue

if res==0:
    if len(v1)>len(v2):
        res = 1
    elif len(v1)<len(v2):
        res = -1

print(res)

Of course, this question can be passed through the judging system more easily, because the lists in python can be directly compared. The encoding is as follows:

indata = input().split()
v1, v2= list(map(int,indata[0].split('.'))) ,list(map(int,indata[1].split('.')))

if v1<v2:
    print(-1)
elif v1>v2:
    print(1)
else:
    print(0)

 

Five, the wizard mouse

Title description

猛兽侠中精灵鼠在利剑飞船的追逐下逃到一个n*n的建筑群中,精灵鼠从(0,0)的位置进入建筑群,建筑群的出
口位置为(n-1,n-1),建筑群的每个位置都有阻碍,每个位置上都会相当于给了精灵鼠一个固定值减速,因为
精灵鼠正在逃命所以不能回头只能向前或者向下逃跑,现在问精灵鼠最少在减速多少的情况下逃出迷宫?

Enter description:

第一行迷宫的大小: n >=2 & n <= 10000;
第2到n+1行,每行输入为以','分割的该位置的减速,减速f >=1 & f < 10。

Output description:

精灵鼠从入口到出口的最少减少速度?

Analysis: This is a simple dynamic programming problem. To find the shortest path, as long as the corresponding dynamic programming table is constructed from bottom to top, an element in the table is the required answer. Knowing from the above rules: the wizard mouse can only move forward or downward. When at the lowest level, the elf mouse can no longer move downwards and can only move forward; when the elf mouse is at the frontmost level, it can only move downwards. The construction rules of the dynamic programming table can be obtained from this. The encoding is as follows:

n = int(input().strip())
building = []
# 构造迷宫
for i in range(n):
    building.append(list(map(int,input().split(','))))

# 构造动态规划表
for i in range(n):
    for j in range(n):
        if j==0 and i==0:
            continue
        if i==0:
            building[n-i-1][n-j-1] += building[n-i-1][n-j]
        elif j==0:
            building[n-i-1][n-j-1] += building[n-i][n-j-1]
        else:
            building[n - i - 1][n - j - 1] += min(building[n-i-1][n-j],building[n-i][n-j-1])

print(building[0][0])

 

6. Print the digital matrix clockwise

Title description

给定一个数字矩阵,请设计一个算法从左上角开始顺时针打印矩阵元素

Enter description:

输入第一行是两个数字,分别代表行数M和列数N;接下来是M行,每行N个数字,表示这个矩阵的所有元素;
当读到M=-1,N=-1时,输入终止。

Output description:

请按逗号分割顺时针打印矩阵元素(注意最后一个元素末尾不要有逗号!例如输出“1,2,3”,而
不是“1,2,3,”),每个矩阵输出完成后记得换行

Analysis: In fact, it only takes four steps to print the digital matrix, and the rest are all cycles. First, the input must be constructed into the form of a two-dimensional array. According to the printing rules, you can first output the data of the first row of the two-dimensional array from left to right, then output the last object element of each row of data from top to bottom, and then Output the last row of data from right to left, and finally output the first object element of each row of data from bottom to top, which is a complete clockwise, and then loop this operation until there is no printable element in the array. The encoding is as follows:

def cal(arr):
    res = []
    while arr:
        # 上左
        res += arr.pop(0)
        # 右下
        if arr and arr[0]:
            for i in arr:
                res.append(i.pop())
        # 下左
        if arr and arr[0]:
            res += arr.pop()[::-1]
        # 左上
        if arr and arr[0]:
            for x in arr[::-1]:
                res.append(x.pop(0))
    # 转换成要求的格式
    res = ','.join(res)
    print(res)


while True:
    m,n = map(int,input().split())
    # 当读到m=-1,n=-1时终止输入
    if m==-1 and n==-1:
        break;
    # 构造输入数据结构
    arr = []
    for i in range(m):
        arr.append(input().split())
    cal(arr)

 

Seven, gashapon machine

Title description

22娘和33娘接到了小电视君的扭蛋任务:
一共有两台扭蛋机,编号分别为扭蛋机2号和扭蛋机3号,22娘使用扭蛋机2号,33娘使用扭蛋机3号。
扭蛋机都不需要投币,但有一项特殊能力:
扭蛋机2号:如果塞x(x范围为>=0正整数)个扭蛋进去,然后就可以扭到2x+1个
扭蛋机3号:如果塞x(x范围为>=0正整数)个扭蛋进去,然后就可以扭到2x+2个
22娘和33娘手中没有扭蛋,需要你帮她们设计一个方案,两人“轮流扭”(谁先开始不限,扭到的蛋可
以交给对方使用),用“最少”的次数,使她们能够最后恰好扭到N个交给小电视君。

Enter description:

输入一个正整数,表示小电视君需要的N个扭蛋。

Output description:

输出一个字符串,每个字符表示扭蛋机,字符只能包含"2"和"3"。

Analysis: According to the rules of No. 2 and No. 3 gashapon machines, just recurse. The encoding is as follows:

num = int(input())
res = ''
while num>0:
    if (num-1)%2==0 :
        res = '2'+res
        num = (num-1)/2
    else :
        res = '3'+res
        num = (num-2)/2
print(res)

 

Guess you like

Origin blog.csdn.net/VinWqx/article/details/104570291