这道题可以使用邻接矩阵来实现朋友圈,并使用字典来将人名与邻接矩阵中的下标对应起来。下面是Python的实现代码:
python
Copy code
# 初始化邻接矩阵
graph = [
[0, 5, 3, 4, 0, 0, 0, 0],
[5, 0, 2, 0, 0, 0, 0, 4],
[3, 2, 0, 0, 4, 0, 0, 0],
[4, 0, 0, 0, 0, 0, 0, 2],
[0, 0, 4, 0, 0, 3, 0, 0],
[0, 0, 0, 0, 3, 0, 6, 0],
[0, 0, 0, 0, 0, 6, 0, 1],
[0, 4, 0, 2, 0, 0, 1, 0]
]
# 人名与邻接矩阵下标的映射字典
name_dict = {
"赵": 0,
"钱": 1,
"孙": 2,
"李": 3,
"周": 4,
"吴": 5,
"郑": 6,
"王": 7
}
# 更新朋友圈
graph[name_dict["赵"]][name_dict["周"]] = 1
graph[name_dict["赵"]][name_dict["郑"]] = 3
graph[name_dict["孙"]][name_dict["李"]] = 5
graph[name_dict["吴"]][name_dict["孙"]] = 5
graph[name_dict["吴"]][name_dict["王"]] = 1
graph[name_dict["郑"]][name_dict["李"]] = 3
graph[name_dict["郑"]][name_dict["吴"]] = 6
# 打印某人的朋友圈
person = input("请输入某人的汉语拼音:")
if person not in name_dict:
print("无此人")
else:
index = name_dict[person]
friends = []
for i, score in enumerate(graph[index]):
if score > 0:
friends.append((score, i))
friends.sort()
print(person + "的朋友圈:")
for score, i in friends:
print(list(name_dict.keys())[list(name_dict.values()).index(i)], end=" ")
print("(" + str(score) + ")", end=" ")
print()
代码中,我们首先使用一个8x8的邻接矩阵来表示朋友圈,其中每个元素表示两个人之间的关系,0表示不认识,大于0的整数表示关系的紧密程度。然后,我们使用一个字典将人名与邻接矩阵中的下标对应起来。接着,我们根据题目给出的数据更新邻接矩阵。最后输入人名获取数据