华为10.16笔试讨论
第一题:
枚举每个数字字母的圈圈个数就可以AC了,写起来麻烦一点,思想简单的。
def main():
num_list = [1, 0, 0, 0, 1, 0, 1, 0, 2, 1]
little_alphabet_list = [1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
0, 0, 0, 1, 0, 0, 0, 0]
larger_alphabet_list = [1, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 0, 1, 0, 0, 0, 0]
s = input()
max_cir = 0
for i in range(len(s)):
if ord(s[i]) - ord("0") >= 0 and ord(s[i]) - ord("0")< 10:
max_cir += num_list[ord(s[i]) - ord("0")]
elif ord(s[i]) - ord("a") >= 0 and ord(s[i]) - ord("a")< 26:
max_cir += little_alphabet_list[ord(s[i]) - ord("a")]
else:
max_cir += larger_alphabet_list[ord(s[i]) - ord("A")]
print(max_cir)
if __name__ == "__main__":
main()
第二题: 先写了个深搜,只过50%,后来写了广搜,只过30%,想不到简化方法了,希望大神指导一下。
深搜代码:
def main():
# input maze
n = int(input())
maze = []
for _ in range(n):
temp = input().split()
maze.append(temp)
#print(maze)
# 优先右下其次左上加快
direction_x = [1, 0, -1, 0]
direction_y = [0, 1, 0, -1]
def BFS(current_point, visited_list, target_point, steps): # calculate min_steps
nonlocal min_steps
if current_point[0] == target_point[0] and current_point[1] == target_point[1]: # 到终点了
#print(steps)
if min_steps > steps:
min_steps = steps
return
if steps >= min_steps:
return
#print(visited_list)
n = len(maze)
for i in range(4):
if current_point[0] + direction_x[i] >= 0 and current_point[0] + direction_x[i] < n:
if current_point[1] + direction_y[i] >= 0 and current_point[1] + direction_y[i] < n:
if maze[current_point[0] + direction_x[i]][current_point[1] + direction_y[i]] == "1":
#print(i)
if [current_point[0] + direction_x[i], current_point[1] + direction_y[i]] not in visited_list:
visited_list.append([current_point[0] + direction_x[i], current_point[1] + direction_y[i]])
BFS([current_point[0] + direction_x[i], current_point[1] + direction_y[i]], visited_list, target_point, steps+1)
visited_list.pop()
# call the BFS function
min_steps = 1000
BFS([0, 0], [[0, 0]], [n-1, n-1], 0)
if min_steps == 1000:
print(-1)
else:
print(min_steps)
if __name__ == "__main__":
main()
广搜代码:
# input maze
n = int(input())
maze = []
for _ in range(n):
temp = input().split()
maze.append(temp)
#print(maze)
# 优先右下其次左上加快
direction_x = [1, 0, -1, 0]
direction_y = [0, 1, 0, -1]
def BFS(current_point, visited_list, target_point, steps): # calculate min_steps
nonlocal min_steps
if current_point[0] == target_point[0] and current_point[1] == target_point[1]: # 到终点了
#print(steps)
if min_steps > steps:
min_steps = steps
return
if steps >= min_steps:
return
#print(visited_list)
n = len(maze)
for i in range(4):
if current_point[0] + direction_x[i] >= 0 and current_point[0] + direction_x[i] < n:
if current_point[1] + direction_y[i] >= 0 and current_point[1] + direction_y[i] < n:
if maze[current_point[0] + direction_x[i]][current_point[1] + direction_y[i]] == "1":
#print(i)
if [current_point[0] + direction_x[i], current_point[1] + direction_y[i]] not in visited_list:
visited_list.append([current_point[0] + direction_x[i], current_point[1] + direction_y[i]])
BFS([current_point[0] + direction_x[i], current_point[1] + direction_y[i]], visited_list, target_point, steps+1)
visited_list.pop()
# call the BFS function
min_steps = 1000
BFS([0, 0], [[0, 0]], [n-1, n-1], 0)
if min_steps == 1000:
print(-1)
else:
print(min_steps)
if __name__ == "__main__":
main()
def main():
# input maze
n = int(input())
maze = []
for _ in range(n):
temp = input().split()
maze.append(temp)
#print(maze)
# 优先右下其次左上会不会快呢
direction_x = [1, 0, -1, 0]
direction_y = [0, 1, 0, -1]
def DFS(queue, layer): # calculate min_steps
next_queue = []
n = len(maze)
#print(queue)
if [n-1, n-1] in queue:
return layer
while len(queue) > 0:
for i in range(4):
if queue[0][0] + direction_x[i] >= 0 and queue[0][0] + direction_x[i] < n:
if queue[0][1] + direction_y[i] >= 0 and queue[0][1] + direction_y[i] < n:
if maze[queue[0][0] + direction_x[i]][queue[0][1] + direction_y[i]] == "1":
next_queue.append([queue[0][0] + direction_x[i], queue[0][1] + direction_y[i]])
queue.pop(0)
return DFS(next_queue, layer+1)
print(DFS([[0, 0]], 0))
if __name__ == "__main__":
main()
第三题题目读起来比较耗时,但是本质思想很简单,排序规则按照他的来,方法用冒泡就可以解决了,因为只有10个人,怎么样都超不了时:
# input maze
n = int(input())
maze = []
for _ in range(n):
temp = input().split()
maze.append(temp)
#print(maze)
# 优先右下其次左上会不会快呢
direction_x = [1, 0, -1, 0]
direction_y = [0, 1, 0, -1]
def DFS(queue, layer): # calculate min_steps
next_queue = []
n = len(maze)
#print(queue)
if [n-1, n-1] in queue:
return layer
while len(queue) > 0:
for i in range(4):
if queue[0][0] + direction_x[i] >= 0 and queue[0][0] + direction_x[i] < n:
if queue[0][1] + direction_y[i] >= 0 and queue[0][1] + direction_y[i] < n:
if maze[queue[0][0] + direction_x[i]][queue[0][1] + direction_y[i]] == "1":
next_queue.append([queue[0][0] + direction_x[i], queue[0][1] + direction_y[i]])
queue.pop(0)
return DFS(next_queue, layer+1)
print(DFS([[0, 0]], 0))
if __name__ == "__main__":
main()
def main():
student_grade = {}
student_name_list = []
for _ in range(10):
input_str = input().split()
student_grade[input_str[0]] = list(map(int, input_str[1: len(input_str)]))
student_name_list.append(input_str[0])
#print(student_name_list)
for i in range(len(student_name_list)): # 冒泡按照他的规则排序
for j in range(i+1, len(student_name_list)):
if sum(student_grade[student_name_list[i]]) < sum(student_grade[student_name_list[j]]): # 总分数比较
student_name_list[i], student_name_list[j] = student_name_list[j], student_name_list[i]
elif sum(student_grade[student_name_list[i]]) == sum(student_grade[student_name_list[j]]): # 比较小分
if student_grade[student_name_list[i]] < student_grade[student_name_list[j]]:
student_name_list[i], student_name_list[j] = student_name_list[j], student_name_list[i]
elif student_grade[student_name_list[i]] == student_grade[student_name_list[j]]: # 所有分数相同时比较名字
if student_name_list[i] > student_name_list[j]:
student_name_list[i], student_name_list[j] = student_name_list[j], student_name_list[i]
count = 3
first_round_name_list = []
first_name_list = [] # 这个变量名应该是final_name_list,写的时候看错了,这里就将就一下吧
before_score = 0
for name in student_name_list:
count_score = 0
for score in student_grade[name]:
if score >= 60:
count_score += 1
if count_score == 3: # 根据用例可以知道如果第二名有5个人,那么这5个人都是第二名,第三名还可以再安排人
first_round_name_list.append(name)
if student_grade[name] == before_score:
first_name_list.append(name)
elif count > 0:
before_score = student_grade[name]
first_name_list.append(name)
count -= 1
# output
print("[First round name list]")
for name in first_round_name_list:
print(name, end = " ")
for score in student_grade[name]:
print(score, end = " ")
print()
print()
print("[Final name list]")
for name in first_name_list:
print(name, end = " ")
for score in student_grade[name]:
print(score, end = " ")
print()
if __name__ == "__main__":
main()