题目来源:牛客网题库/他人笔经/自己笔试
使用编程语言:Python
Tips:除牛客网题库的题目外,其他题目只使用了1到2个测试用例,且全是自己的思路,可能存在没考虑到的情况或者使用不好的算法,并不一定是正确的,欢迎指正。
1.字符串模糊匹配
来源:牛客网题库
从字符串string开始完整匹配子串sub,返回匹配到的字符个数。
sub中如果出现'?'表示可以匹配一到三个除' '以外的任意字符。
如果sub还有找不到匹配的字符,则说明不能完整匹配。
如果能完整匹配,返回匹配到的字符个数,如果有多种匹配方式,返回匹配字符数最少的那个,如果不能完整匹配,返回-1。
这道题没有做过多思考,直接想用正则解,可是忘记正则语法了,上网查手册做的。
import re string = input() sub = input() sub = sub.replace('?','.{1,3}') rep = re.compile('^'+sub)#创建模式对象 res = re.findall(rep,string)#返回是数组形式 if res:#寻找最少字符数 l = len(res[0]) for i in res: a = len(i) if a<l: l = a print(l) else: print(-1)
2.矩阵顺时针旋转90度
题目来源:牛客网笔经
import numpy as np from numpy import * #自己定义一个输入 A = array([[0,12,5,68,40],[2,4,5,8,4],[1,78,5,6,2]]) a = min(A.shape[1],A.shape[0]) B = np.ones((A.shape[1],A.shape[0])) for i in range(a): B[:,a-1-i] = A[i,:] print(B)
3.宠物评分
题目来源:牛客网笔经
宠物有4个属性,攻防生命和评分,评分根据攻防生命得到,为攻*攻击系数+防*防御系数+生命*生命系数得到。给2个宠物的所有属性,给第3个宠物的攻防生命,求第3个宠物评分。得不出就输出0。
一个思路是矩阵经过线性变换后,就知道能否用前两行向量表示第三行,我的思路是直接调用sympy的解方程的函数solve():
import numpy as np from sympy import * from sympy.abc import x,y,z,a,b,c x1,y1,z1,D1 = map(int,input("请输入宠物属性和评分(第一个):").split()) x2,y2,z2,D2 = map(int,input("请输入宠物属性和评分(第二个):").split()) print(x1,y1,z1,D1,x2,y2,z2,D2) x3,y3,z3 =map(int,input("请输入宠物属性(第三个):").split()) aa = solve([a*x1+b*y1+c*z1-D1,a*x2+b*y2+c*z2-D2],[a,b,c]) A = aa[a] B = aa[b] print(A,B) bb = solve([A*x3+B*y3+c*z3-x],[x]) answer = str(bb[x]) if answer.isnumeric(): print("第三个宠物评分为",answer) else: print(0)
这个题目就是解三元一次方程,但是只用两组已知量,很多情况看上去可以找到规律,但是解其实有无穷个,所以输出0.
4.输出进度
题目来源:牛客网笔经
有一个进度条,一开始时预测n帧能走完,进度条要从0走到1,但是在其中某些帧中情况突变,发现需要b帧才能走完,请输出每帧进度条的值,其中输出时要求四舍五入且保留两位小数:
测试用例:初始假设n帧,并且接下来有k条记录,这k条(a,b)记录分别为第a帧时情况改变,此时预测进度条结束时的帧为第b帧
/*输入:
10 3
6 12
4 8
8 10
输出:
0.10
0.20
0.30
0.40
0.55
0.70
0.75
0.80
0.90
1.00
import numpy as np n,k = map(int,input("输入初始预测帧数和变化次数:").split()) A = np.zeros((k,2)) for i in range(k): a,b = map(int,input("请输入变化帧数的位置和此时预测的结束帧:").split()) if a>=b: print("不符合常理,结束") if a<b: A[i,0] = a A[i,1] = b for i in range(k): for j in range(k): if i!=j and A[i,0]==A[j,0]: print("变化冲突,结束") break B = sorted(A,key = lambda x:x[0]) C = np.matrix(B) print(C) count = 0 #ProBar = [] N = [0] M = [n] for j in range(k): N.append(int(C[j,0])) M.append(int(C[j,1])) N.append(int(C[k-1,1])) for x in range(k+1): rest = 1-count for i in range(N[x+1]-N[x]): count += rest/(M[x]-N[x]) print(count)
5.矩阵消消乐
给一个矩阵,矩阵中为任意正整数值,若行列中存在连续三个以上的相同的值则消除,行列允许共用一个节点,问一次消除后,矩阵中还剩下多少个数。
例如:
输入:
5 2 2 4 8 9 5 3 5
3 2 2 5 7 5 2 1 2
1 2 2 2 1 1 3 2 1
2 2 5 1 2 5 8 1 8
6 1 2 3 7 9 5 6 5
4 3 2 1 5 8 4 5 4
8 4 5 5 4 4 5 8 7
2 1 3 2 1 5 8 7 9
9 2 4 9 8 7 5 3 1
输出:81-8=73
import numpy as np from collections import Counter #随意生成一个矩阵用作测试 a = np.random.randint(0,10,(5,6)) #一个测试用例 b = np.array([[5, 2, 2, 4, 8, 9, 5, 3, 5],
[3, 2, 2, 5, 7, 5, 2, 1, 2],
[1, 2, 2, 2, 1, 1, 3, 2, 1],
[2, 2, 5, 1, 2, 5, 8, 1, 8],
[6, 1, 2, 3, 7, 9, 5, 6, 5],
[4, 3, 2, 1, 5, 8, 4, 5, 4],
[8, 4, 5, 5, 4, 4, 5, 8, 7],
[2, 1, 3, 2, 1, 5, 8, 7, 9],
[9, 2, 4, 9, 8, 7, 5, 3, 1]]) A = b B = np.zeros((A.shape[0],A.shape[1])) x=A.shape[1] #6 y=A.shape[0] #3 for i in range(y): for j in range(x): if i-1>=0 and A[i,j]==A[i-1,j]:#与上面的相同 if i-2>=0 and A[i,j]==A[i-2,j]: B[i,j]=B[i-1,j]=B[i-2,j]=1 if i+1<=y-1 and A[i,j]==A[i+1,j]: B[i,j]=B[i-1,j]=B[i+1,j]=1 if i+1<=y-1 and A[i,j]==A[i+1,j]:#与下面的相同 if i-1>=0 and A[i,j]==A[i-1,j]: B[i,j]=B[i-1,j]=B[i+1,j]=1 if i+2<=y-1 and A[i,j]==A[i+2,j]: B[i,j]=B[i+2,j]=B[i+1,j]=1 if j-1>=0 and A[i,j]==A[i,j-1]:#与左面的相同 if j-2>=0 and A[i,j]==A[i,j-2]: B[i,j]=B[i,j-1]=B[i,j-2]=1 if j+1<=x-1 and A[i,j]==A[i,j+1]: B[i,j]=B[i,j-1]=B[i,j+1]=1 if j+1<=x-1 and A[i,j]==A[i,j+1]:#与右面的相同 if j+2<=x-1 and A[i,j]==A[i,j+2]: B[i,j]=B[i,j+1]=B[i,j+2]=1 if j-1>=0 and A[i,j]==A[i,j-1]: B[i,j]=B[i,j-1]=B[i,j+1]=1 print(A) print(B) count=0 for i in range(y): for j in range(x): if B[i,j]==1: count += 1 print (x*y-count)
6.权重迷宫路径
m*n矩阵作为迷宫,每个点的值代表权重,只能上下左右走。从(0,0)走到(m,n),权重值最小为多少。
题目来源:在线笔试
这个题目是我笔试时做的,在IDE上写得代码如下,当时很紧张也很急于做出来,暴力循环做的,没有考虑如果值存在负数会不会有绕路走的可能,做完后也不知道该用什么好方法解:
import numpy as np import random #我自己写的测试用例 A = np.matrix([[1,2,4,5,8,12],[2,5,4,86,1,1],[48,48,5,21,4,9]]) x = A.shape[1]#6 y = A.shape[0]#3 road = [] i = 0 j = 0 foo = [0,1] u=0 count = A[0,0] while u<=10000: R = random.choice(foo) if i<y-1 and R==0: i += 1 count += A[i,j] if j<x-1 and R==1: j += 1 count +=A[i,j] if i==y-1 and j==x-1: road.append(count) i = 0 j = 0 u += 1 count = A[0,0] print(min(road))