python实现代码
#回收站选址
def condition(point, points):
"""
检查是否满足条件二
:param point: 元组坐标(x,y)
:param points: 元组列表,包含所有垃圾点
"""
if (point[0], point[1]+1) in points and (point[0], point[1]-1) in points and (point[0]+1, point[1]) in points and (point[0]-1, point[1]) in points:
return True
else:
return False
def getScores(point, points):
"""
计算可行地址的得分
:param point: 可行地址的元组坐标(x,y)
:param points: 元组列表,包含所有垃圾点
"""
score = 0
if (point[0]+1, point[1]+1) in points:
score += 1
if (point[0]+1, point[1]-1) in points:
score += 1
if (point[0]-1, point[1]+1) in points:
score += 1
if (point[0]-1, point[1]-1) in points:
score += 1
return score
if __name__ == "__main__":
n = int(input("请输入垃圾点总数:"))
points = [] #记录所有垃圾点的坐标地址
scores = [0,0,0,0,0] #记录每种得分的地址个数
for i in range(n):
x, y = map(int, input("请输入每个垃圾点的坐标:").split())
points.append((x,y))
for point in points:
if condition(point, points):
score = getScores(point, points)
scores[score] += 1
for i in range(5):
print("得分为 %d 的回收站选址个数为 %d" %(i, scores[i]))