2022/7/23 科大讯飞算法岗笔试
1.排序然后看相邻的元素之差是否大于4,太水了。。。
n=int(input()) a=[] for i in range(n): a.append(int(input())) a.sort() flag=True for i in range(1,len(a)): if a[i]-a[i-1]>4: flag=False break a=[str(v) for v in a] print(' '.join(a)) if flag: print(1) else: print(0)
2.leetcode 原题 基本计算器III
def solve(s): global idx stk=[] num=0 sign='+' f=lambda x:ord(x)-ord('0') while idx<len(s): c=s[idx] if c.isdigit(): num=num*10+f(c) if c=='(': idx+=1 num=solve(s) if (not c.isdigit() and c!=' ')&nbs***bsp;idx==len(s)-1: pre=0 if sign=='+': stk.append(num) elif sign=='-': stk.append(-num) elif sign=='*': pre=stk.pop() stk.append(pre*num) elif sign=='/': pre=stk.pop() stk.append(pre//num) sign=c num=0 if c==')': break idx+=1 return sum(stk) idx=0 s=input() print(solve(s))3.给9个样本,利用朴素贝叶斯对接下来输入的样本进行分类
buf=[[1,2,0,2,1], [0,1,1,2,1], [1,0,1,1,1], [1,2,1,0,1], [1,0,0,1,0], [0,0,0,1,0], [0,1,1,1,0], [0,0,0,0,0], [1,0,0,0,0]] px=[[[0]*2 for j in range(3)] for i in range(4)] for i in range(4): for j in range(3): cnt1=0 cnt0=0 for v in buf: if v[i]==j and v[-1]==1: cnt1+=1 if v[i]==j and v[-1]==0: cnt0+=1 px[i][j][1]=cnt1/4 px[i][j][0]=cnt0/5 x=eval(input()) ans=[4/9,5/9] for i in range(4): for k in range(2): ans[k]*=px[i][x[i]][k] if ans[0]>ans[1]: print(0) else: print(1)