赞
踩
又是一年春招时,有幸收到华为自动驾驶算法岗,之前刷题不多,在此汇总下牛客网的真题,主要采用Python编写,个人觉得语言只是实现工具而已,并不是很关键,Python简洁易懂,更加适合算法工程师快速验证idear,避免重复造轮子的繁琐,希望对看合集的你有些许帮助!
s0,s1,s3,s2 = input(), input().split(), input(), input().split()
s = map(str,sorted(map(int, set(s1+s2))))
print(''.join(s))
st1 = set(input())
st2 = set(input())
flag = 0
for i in st1:
if i not in st2:
flag +=1
print('true') if flag ==0 else print('false')
解题思路:将真分数(a/b)拆分成a*(1/b),也就是a个1/b,然后我们从大到小依次找出a中能被B整除的数.
比如: 5/8 这个真分数首先我们看,5不能被8整除,但是4可以,那从5中把4分离出来,变成1+4,剩余的1不能拆分,那埃及分数就是1/8+1/2
while True:
try:
a,b = map(int,input().split('/'))
a = a * 10
b = b * 10
res = []
while a:
for i in range(a,0,-1):
if(b % i == 0):
res.append('1' + '/' + str(int(b / i)))
a = a - i
break
print('+'.join(res))
except:
break
while True:
try:
m, n = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x, y = int(input()), int(input())
x3, y3 = map(int, input().split())
print(0 if m <= 9 and n <= 9 else -1)
print(0 if x1 < m and x2 < m and y1 < n and y2 < n else -1)
print(0 if 9 > m > x else -1)
print(0 if 9 > n > y else -1)
print(0 if x3 < m and y3 < n else -1)
except:
break
lst = input()
n = 0
for i in lst:
if ord("A") <= ord(i) <= ord("Z"):
n += 1
print(n)
while True:
try:
s = input()
res = []
for i in range(len(s)):
for j in range(i+1, len(s)+1):
if s[i:j] == s[i:j][::-1]:
res.append(j-i)
if res != '':
print(max(res))
except:
break
n = int(input())
n = bin(n)[2:] # 去除掉 0b
n = n.split('0')
ls = []
for i in n:
ls.append(i.count('1'))
print(max(ls))
while True: try: s = input() sc = 0 # 密码长度 if len(s) <= 4: sc = sc + 5 elif len(s) <= 7: sc = sc + 10 else: sc = sc + 25 # 字母 isu = 0 isl = 0 for i in s: if i.isupper(): isu = 1 break for i in s: if i.islower(): isl = 1 break sc = sc + 10 * (isu + isl) # 数字 shu = '0123456789' count1 = 0 for i in s: if i in shu: count1 = count1 + 1 if count1 == 1: sc = sc + 10 elif count1 > 1: sc = sc + 20 else: sc = sc # 符号 fh = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' count2 = 0 for i in s: if i in fh: count2 = count2 + 1 if count2 == 1: sc = sc + 10 elif count2 > 1: sc = sc + 25 else: sc = sc # 奖励 if isu + isl == 2 and count1 >= 1 and count2 >= 1: sc = sc + 5 elif isu + isl > 1 and count1 >= 1 and count2 >= 1: sc = sc + 3 elif isu + isl > 1 and count1 >= 1: sc = sc + 2 if sc >= 90: print("VERY_SECURE") elif sc >= 80: print("SECURE") elif sc >= 70: print("VERY_STRONG") elif sc >= 60: print("STRONG") elif sc >= 50: print("AVERAGE") elif sc >= 25: print("WEAK") elif sc >= 0: print("VERY_WEAK") except: break
dic = { '3' : 1, '4' : 2, '5' : 3, '6' : 4, '7' : 5, '8': 6, '9' : 7, '10' : 8, 'J' : 9, 'Q' : 10, 'K' : 11, 'A' : 12, '2' : 13, 'joker' : 14, 'JOKER' : 15 } def isboom(lst): if len(lst) == 4 and len(set(lst)) == 1: return True return False while True: try: s1, s2 = input().split('-') lst1, lst2 = s1.split(), s2.split() L1, L2 = len(lst1), len(lst2) if L1 == L2: if dic[lst1[0]] > dic[lst2[0]]: print(s1) else: print(s2) else: if 'joker JOKER' in (s1, s2): print('joker JOKER') elif isboom(lst1): print(s1) elif isboom(lst2): print(s2) else: print('ERROR') except: break
a = input().split(' ') dictory = {'J':11,'Q':12,'K':13,'A':1} def dfs(wait,target,out): if len(wait) == 1: if wait[0] in dictory: c = dictory[wait[0]] else: c = int(wait[0]) if target == c: L.append(wait[0]+out) else: for i in range(len(wait)): w = wait[:i]+wait[i+1::] if wait[i] in dictory: c = dictory[wait[i]] else: c = int(wait[i]) dfs(w,target-c,'+'+wait[i]+out) dfs(w,target+c,'-'+wait[i]+out) dfs(w,target*c,'/'+wait[i]+out) dfs(w,target/c,'*'+wait[i]+out) L = [] if 'joker' in a or 'JOKER' in a: print('ERROR') else: dfs(a,24,'') if not L: print('NONE') else: print(L[0])
ip = input().split(".")
if len(ip) != 4: # 排除长度不为4部分的
print("NO")
else:
for i in ip:
if (not i.isdigit() # 排除不是数字的
or (len(i) > 1 and i[0] == "0") # 排除首位为0的
or int(i) < 0 # 排除在[0,255]范围外的
or int(i) > 255
):
print("NO")
break
else:
print("YES")
迭代方法:
def func(x,y):
if x < 0 or y < 0:
return 0
elif x == 0 or y == 0:
return 1
else:
return func(x-1, y)+func(x, y-1)
while True:
try:
a,b = map(int,input().split())
c = func(a, b)
print(c)
except:
break
动态规划解决
while True:
try:
n,m = map(int, input().split(' '))
dp = [[1 for i in range(n+1)] for j in range(m+1)]
for i in range(1,m+1):
for j in range(1,n+1):
dp[i][j] = dp[i-1][j]+dp[i][j-1]
print(dp[m][n])
except:
break
采用遍历方法:
while True: try: s = input() for c in s: if not c.isdigit(): s = s.replace(c, " ") s = s.split() max_flag = 0 res = "" for c in s: if len(c)>max_flag: max_flag = len(c) for c in s: if len(c)==max_flag: res = res+c print(str(res)+','+str(max_flag)) except: break
1.首先计算3,5倍数的差值,如果3的倍数为+,那么5的倍数为-,反之亦然。把结果放入set{}集合中 2.分别取剩余数值,与之前结果计算,每个数值都可以为+或者-,那么把+,-的结果均放入集合中 3.全部计算完成后,如果0值在结果里,则返回true,否则返回false while True: try: length = int(input()) num_list = list(map(int, input().strip().split())) res = 0 # 计算初始3,5倍数的差值 for _ in range(length): i = num_list.pop(0) if i % 3 == 0: res += i elif i % 5 == 0: res -= i else: num_list.append(i) res = {res} # 结果计算,把之前结果,分别计算当前值的+和- 2种情况,然后把结果再放回去,给下一次计算 while num_list: i = num_list.pop(0) res_plus = [x + i for x in res] res_plus.extend([x - i * 2 for x in res_plus]) res = set(res_plus) # 最后如果0值在结果中,表示可以算出,如果不在则不行 if 0 in res: print('true') else: print('false') except: break
n1, lst1, n2, lst2 = int(input()), input().split(' '), int(input()), input().split(' ')
n = 0
lst=[]
for i in lst2:
if i not in lst1:
n += 1
for j in range(len(lst1)):
print(lst1[j],':', lst2.count(lst1[j]))
print('Invalid :',n)
import re rmb_list = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖','拾','佰','仟','万','亿'] rmb_list2 = ['','拾','佰','仟','万','拾','佰','仟','亿'] s = input().split('.') s1 = s[0][::-1] s2 = s[1] res = '' # 计算小数点前面 if int(s1)>0: for i in range(len(s1)): if s1[i] == '0': res = rmb_list[int(s1[i])] + res else: res = rmb_list[int(s1[i])] + rmb_list2[i] + res res = res.replace('壹拾','拾') tmp = re.findall(r'[\'零\']{2,20}',res) if tmp: for t in tmp: res = res.replace(t,'零') if res[-1] == '零': res = res[:-1] res += '元' # 计算小数点后面 if int(s2) == 0: res += '整' elif int(s2) >= 10: if int(s2[1]) != 0: res += rmb_list[int(s2[0])] +'角' + rmb_list[int(s2[1])] + '分' else: res += rmb_list[int(s2[0])] +'角' else: res += rmb_list[int(s2[1])] +'分' res = '人民币' + res print(res)
s = input() s_o = '' char_pre = '' for i in s: if i.isdigit() : #遇到数字,判断其前面是否非数字,是则表示数字的开始,先插入‘*’ if char_pre.isdigit() != True: s_o +='*' else: #非数字情况,判断其前是否为数字,是则表示数字结束,插入‘*’ if char_pre.isdigit(): s_o +='*' s_o += i #把当前字符带出来 char_pre = i #当前字符更新到 前字符 if s[-1].isdigit(): #结束的时候,判断是否数字结束,如果是的话,插入‘*’ s_o +='*' print(s_o)
a = int(input()) lst = input().split() a = 0 # 小于0 的数量 b = 0 # 大于0 的数量、 sum = 0.0 for i in lst: i = int(i) if i<0: a +=1 elif i > 0: b += 1 sum += i print(a, '%.1f'%(sum/b)) if b > 0 else print(a, 0.0)
def f(pq): w10, w5, w2, w1 = 0, 0, 0, 0 # 记录已经找出的零钱 while pq > 0: # 循环直到找零完成 if pq >= 10 and dic_q['10'] >= 1: # 可以找10元时 pq -= 10 # 余额减10 w10 += 1 # 已经找出的零钱+1 dic_q['10'] -= 1 # 零钱10数量-1 elif pq >= 5 and dic_q['5'] >= 1: # 可以找5元时 pq -= 5 w5 += 1 dic_q['5'] -= 1 elif pq >= 2 and dic_q['2'] >= 1: pq -= 2 w2 += 1 dic_q['2'] -= 1 elif pq >= 1 and dic_q['1'] >= 1: pq -= 1 w1 += 1 dic_q['1'] -= 1 else: pq -= 1 # 耍赖,如果因零钱不足导致不能退币,则尽最大可能退币,以减少用户损失。 return pq, w1, w2, w5, w10 while True: try: s = input().split(';') dic_m = {'A1': 2, 'A2': 3, 'A3': 4, 'A4': 5, 'A5': 8, 'A6': 6} # 商品单价字典 dic_n = {'A1': 0, 'A2': 0, 'A3': 0, 'A4': 0, 'A5': 0, 'A6': 0} # 商品数量字典 dic_q = {'10': 0, '5': 0, '2': 0, '1': 0} # 零钱字典 pq = 0 for i in s[:-1]: if i[0] == 'r': # 系统初始化,把商品数量和零钱放入字典 b = i.split() m = b[1].split('-') q = b[2].split('-') dic_n['A1'], dic_n['A2'], dic_n['A3'], dic_n['A4'], dic_n['A5'], dic_n['A6'] = int(m[0]), int(m[1]), int(m[2]), int(m[3]), int(m[4]), int(m[5]) dic_q['1'], dic_q['2'], dic_q['5'], dic_q['10'] = int(q[0]), int(q[1]), int(q[2]), int(q[3]) print('S001:Initialization is successful') elif i[0] == 'p': # 投币 pq1 = int(i.split()[1]) if pq1 not in [1, 2, 5, 10]: # 币值非法 print('E002:Denomination error') elif pq1 not in [1, 2] and pq1 >= (dic_q['1'] + dic_q['2']*2): # 存钱盒中1元和2元面额钱币总额小于本次投入的钱币面额 print('E003:Change is not enough, pay fail') elif dic_n['A1'] == 0 and dic_n['A2'] == 0 and dic_n['A3'] == 0 and dic_n['A4'] == 0 and dic_n['A5'] == 0 and dic_n['A6'] == 0: # 自动售货机中商品全部销售完毕 print('E005:All the goods sold out') else : dic_q[str(pq1)] += 1 # 字典对应币值零钱数量加一 pq += pq1 # 投币余额增加 print('S002:Pay success,balance={}'.format(pq)) elif i[0] == 'b': # 购买商品 bn = i.split()[1] if bn not in dic_n.keys(): # 购买的商品不在商品列表中 print('E006:Goods does not exist') elif dic_n[bn] == 0: # 所购买的商品的数量为0 print('E007:The goods sold out') elif int(pq) < dic_m[bn]: # 投币余额小于待购买商品价格 print('E008:Lack of balance') else: pq = int(pq) - dic_m[bn] # 余额相应减少 print('S003:Buy success,balance={}'.format(pq)) dic_n[bn] -= 1 # 贩卖机物品数量减一 elif i[0] == 'c': if pq == 0: # 币余额等于0 print('E009:Work failure') else: # 按照退币原则进行“找零” pq, w1, w2, w5, w10= f(pq) # f()函数实现过程 print('1 yuan coin number={}'.format(w1)) print('2 yuan coin number={}'.format(w2)) print('5 yuan coin number={}'.format(w5)) print('10 yuan coin number={}'.format(w10)) elif i[0] == 'q': # 查询功能 if ' ' not in i: # 给出的案例中q1之间无空格,非标准输入。为了过示例添加 print('E010:Parameter error') elif i.split()[1] not in ['0', '1']: # “查询类别”参数错误 print('E010:Parameter error') elif i.split()[1] == '0': # 查询类别0 print('A1 2 {}'.format(dic_n['A1'])) print('A2 3 {}'.format(dic_n['A2'])) print('A3 4 {}'.format(dic_n['A3'])) print('A4 5 {}'.format(dic_n['A4'])) print('A5 8 {}'.format(dic_n['A5'])) print('A6 6 {}'.format(dic_n['A6'])) elif i.split()[1] == '1': # 查询类别1 print('1 yuan coin number={}'.format(dic_q['1'])) print('2 yuan coin number={}'.format(dic_q['2'])) print('5 yuan coin number={}'.format(dic_q['5'])) print('10 yuan coin number={}'.format(dic_q['10'])) except: break
a = int(input())
n = 0
for s in range(a +1):
ss = s**2
b = str(s)
bb = str(ss)
if b == bb[-len(b):]:
n +=1
print(n)
a = int(input())
b = 2*a + 1.5 * a * (a-1)
print(int(b))
a = int(input())
lst = [int(i) for i in input().split()]
# lst = list(map(int, input().split())) # 需要转成整数,才能排序
b = input()
if b == "0":
lst = sorted(lst, reverse=False)
elif b == "1":
lst = sorted(lst, reverse=True)
for j in lst:
print(j, end=' ')
while True:
try:
a = input()
s = sorted(set(a)) # reverse=True 降序操作
ss = sorted(s, key=lambda x:a.count(x),reverse=True)
print(''.join(ss))
except:
break
这道题也可以看作“动态规划问题”:求升序排列的子数组最大长度
创建一个全为1的n列数组,本题为 [1,1,1,1,1] 创建双层循环,外层遍历n位数字倒序,内层遍历第n位后的子数组,判断子数组中的数字是否大于当前数字,如果有大于当前数字的数字,则说明还有递增的可能性,因此求 L[j] 的最大值+1;如果没有比当前数字大的数,说明从当前位出发,没有再递增的可能性,所以L[i]=1,保持不变。 将上述逻辑整理为代码如下:
while True: try: n = int(input()) s = [int(x) for x in input().split()] L = [1]* n for i in reversed(range(n)): for j in range(i+1, n): if s[j] > s[i]: L[i] = max(L[i], L[j] + 1) max_len = max(L) print(max_len) except: break
lst = [] num = [0, 0] p = 0 while True: try: lst.append(input()) except: break for i in lst: i = int(i) if i < 0: p += 1 elif i == 0 : continue else: num[0] += 1 # 数量 num[1] += i # sum if num[0] != 0: print(p) print(f'%.1f'%(num[1] / num[0] )) else: print(p) print(0.0)
string = input()
print(string[::-1])
# 法一:牛顿迭代法 while True: try: a = float(input().strip()) # 获取输入的实数a e = 0.0001 # 设定一个精度值 t = a # 初始化立方根t的值为输入的值a while abs(t*t*t - a) > e: # 差值没有达到精度,便一直更新立方根 # x(i+1) = x(i) - f(xi)/f'(xi) # 更新后的x = 原x - (原x的立方-a)/f(原x)导数 t = t - (t*t*t - a) * 1.0 / (3 * t*t) print("%.1f" % t) # 当精度达到要求时,此时的立方根t便为输入实数的立方根解 except: break # 法二:二分法 while True: try: a = float(input().strip()) epsilon = 0.0001 low = min(-1.0, a) high = max(1.0, a) ans = (low + high)/2 while abs(ans**3 - a) >= epsilon: if ans**3 < a: low = ans else: high = ans ans = (low + high)/2.0 print('%.1f' % ans) except: break
在大的数的倍数里面去找最小的能整除另外一个数的数,就是最小公倍数,按照大的来找,循环次数能够降到很少
lst = input().split()
a ,b= int(lst[0]),int(lst[1])
if a<b:
a,b = b, a
for i in range(a, a*b+1, a):
if i % b ==0:
print(i)
break
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。