滴滴 8.21 笔试 AK代码
滴滴8.21笔试算法部分的代码,仅供参与考试的同学参考。
n=int(input()) a=[] for _ in range(2): a.append(list(input().split())) res1,res2=None,None pre1=set() hou1=set() pre2=set() hou2=set() flag=0 idx1=None idx2=None for i in range(len(a[0])): if a[0][i]=='X': flag=1 idx1=i continue if flag==0: pre1.add(a[0][i]) else: hou1.add(a[0][i]) flag=0 for i in range(len(a[1])): if a[1][i]=='X': flag=1 idx2=i continue if flag==0: pre2.add(a[1][i]) else: hou2.add(a[1][i]) #print(pre1,hou1,pre2,hou2,idx1,idx2) res1=min(idx1,idx2)+1 res2=max(idx1,idx2)+1 #print(res1,res2) if idx1>idx2: count=0 for x in pre2: if x in hou1: count+=1 res1-=count c=0 for x in hou1: if x in pre2: c+=1 res2+=c elif idx1<idx2: count=0 for x in pre1: if x in hou2: count+=1 res1-=count c=0 for x in hou2: if x in pre1: c+=1 res2+=c else: count1=0 for x in pre2: if x in hou1: count1+=1 count2=0 for x in pre2: if x in hou1: count2+=1 res1=res1-max(count1,count2) c1=0 for x in hou1: if x in pre2: c1+=1 c2=0 for x in hou1: if x in pre2: c2+=1 res2+=max(c1,c2) print(res1,res2)2.
n=int(input()) res=[[0]*n for _ in range(n)] a=[] cache={} def fib(n): if n in cache: return cache[n] if n==0&nbs***bsp;n==1: return 1 res=fib(n-1)+fib(n-2) cache[n]=res return res for i in range(n*n): a.append(fib(i)) a=a[::-1] i,j,count,f=0,0,0,0 vis=[[0]*n for _ in range(n)] nextt=[(0,1),(1,0),(0,-1),(-1,0)] def isin(i,j): return i>=0 and i<n and j>=0 and j<n while count<n*n: if count==n*n-1: res[i][j]=a[count] break vis[i][j]=1 res[i][j]=a[count] ni=i+nextt[f][0] nj=j+nextt[f][1] if not isin(ni,nj)&nbs***bsp;vis[ni][nj]==1: f=(f+1)%4 continue i=ni j=nj count+=1 for x in res: s='' for y in x: s=s+str(y)+' ' print(s[:-1])