华为算法笔试
基础信息
岗位:
AI算法工程师
编程语言:
python为主
c++,JAVA为辅
2021
购物管理(8月)
(leetcode-860)
def sol(bills):
five, ten = 0, 0
for i in range(len(bills)):
## 付的是5元,直接收
if bills[i] == 5:
five += 1
## 付的是10元,收了,找5元
elif bills[i] == 10:
ten += 1
five -= 1
## 付的是20,收了
elif ten > 0:# 如果有10元,找10+5元
ten -= 1
five -= 1
else:# 如果没有10元,找5+5+5元
five -= 3
## 需要提前支出5,才行
if five < 0:
return False
return 'True,'+str(i+1)
if __name__ == "__main__":
bills = list(map(int,input().split()))
res = sol(bills)
print(res)
https://leetcode-cn.com/problems/lemonade-change/
https://www.jianshu.com/p/48fda0b054b8
https://blog.csdn.net/zhoutianzi12/article/details/107968622
满足强迫症(8月)
def sol(matrix,x,y,s,vis):
# 超出边界
if not 0 <= x < m or not 0 <= y< n:
return False
# 走到0,或者,已经访问过
if matrix[x][y] == 0 or vis[x][y] == 1:
return False
# 走到终点
if x == m-1 and y == n-1:
return True
# 记录当前点
vis[x][y] = 1
# 尝试4个方向,只要有1个方向能走通,就行
for nx,ny in [(x-s,y),(x+s,y),(x,y-s),(x,y+s)]:
if sol(matrix,nx,ny,s,vis):
return True
# 四个方向都走不通,放弃记录当前点
vis[x][y] = 0
return False
if __name__ == "__main__":
step = int(input())
m, n = list(map(int,input().split()))
matrix = []
for _ in range(m):
row = list(map(int,input().split()))
matrix.append(row)
visited = [[0]*n for _ in range(m)]
res = sol(matrix,0,0,step,visited)
print(res)
https://www.jianshu.com/p/48fda0b054b8
https://blog.csdn.net/zhoutianzi12/article/details/107968622
x形输出字符串(8月)
def sol(s,n):
arr = ['' for _ in range(n)]
loop = n // 2
i, j = 0, 0
while i < len(s):
## X形中心交点
if j == loop:
arr[j] += s[i]
i += 1
## X形左半边
elif j < loop:
# 矩阵第j列,存在输出的第j个字符串中
arr[j] += s[i]
i += 1
if i == len(s): break
# 矩阵对称的第~j(-j-1)列,存在输出的第~j个字符串中
arr[~j] += s[i]
i += 1
## X形右半边
elif j > loop:
arr[~j] += s[i]
i += 1
if i == len(s): break
arr[j] += s[i]
i += 1
j = (j+1)%(n-1)
print(arr)
return ''.join(arr)
if __name__ == "__main__":
s,n = input().split(',')
n = int(n)
res = sol(s,n)
print(res)
https://blog.csdn.net/zhoutianzi12/article/details/107968622
https://www.nowcoder.com/discuss/475527
https://www.jianshu.com/p/48fda0b054b8
分糖果(7月)
code有问题
# 递归
def sol(i):
if i >= 0:
return li[i] ^ sol(i - 1)
return 1
# 输入
n = int(input("请输入:"))
li = []
for i in range(n):
a = int(input("请输入第%d个的质量" % i))
li.append(a)
print(li)
xor = sol(len(li)-1) ^ 1
if xor:
minm = min(li)
print('小李的质量为%d' % minm)
summ = 0
for i in li:
summ += i
print("小明的质量为%d" % (summ-minm))
else:
print(False)
https://www.nowcoder.com/discuss/450154?channel=666&source_id=subject
https://ask.csdn.net/questions/1091009
在线学习系统(7月)
def sol(nums):
page,summ = 0,0
for i in nums:
summ += i
if summ > 60:
summ %= 60 # 丢掉60的倍数
page = 0
page += 1
if page > 4:
return 0
return 1
n_rcd = int(input())
res = []
for _ in range(n_rcd):
rcd = list(map(int,input().split()))
n, times = rcd[0],rcd[1:]
res.append(sol(times))
for i in res:
print(i)
https://blog.csdn.net/qq_35353824/article/details/107544329
字符串修正(6月)
https://blog.csdn.net/qq_35353824/article/details/106819386
手牌的最大权重(4月)
https://www.nowcoder.com/discuss/398098?channel=666&source_id=subject