Python笔记-CSP 201912-2 回收站选址

    科技2024-01-18  93

    文章目录

    前言一、题目二、算法思想三、代码

    前言

    一、题目

    二、算法思想

    大概的题意是给我们一些垃圾对应的坐标,然后从中判断出来哪些可以作为回收站的并对这些回收站进行评分判断,最后输出0~4分各个分数段的回收站数目。 判断回收站和评分回收站的方式不难,以下图为例若当前判断(x,y)这个垃圾点如果这个点要符合回收站的话,就必须四个红色坐标都存在垃圾点才行。对于一个回收站的评分来说,四个角即黑色坐标的点的个数,就代表了坐标为(x,y)回收站的评分。

    在代码实现中使用的存储结构为字典、列表、元组坐标以元组的方式存入列表,方便之后的访问而回收站的评分使用了字典来存储。判断回收站和回收站的评分都使用了in方法,先对(x,y+1)、(x,y-1)、(x+1,y)和(x-1,y)四个垃圾点判断是否存在若都存在时再判断(x+1,y+1)、(x+1,y-1)、(x-1,y+1)和(x-1,y-1)四角的垃圾点判断并计算存在的个数最后将对应评分的回收站个数加1,打印各个评分段回收站数量即可。

    三、代码

    # coding=utf-8 # 作者: 小狐狸 # 题目: 回收站选址 n = int(input()) pos = [] #垃圾点位置 score = {} #回收站各评分数量 score[0] = 0 score[1] = 0 score[2] = 0 score[3] = 0 score[4] = 0 for i in range(n): x,y = input().split() x = int(x) y = int(y) pos.append((x,y)) #以元组方式存入 for station in pos: #遍历每个垃圾点 x = station[0] y = station[1] #对每一个垃圾点进行回收站的判断 if (x,y+1) not in pos: continue if (x,y-1) not in pos: continue if (x+1,y) not in pos: continue if (x-1,y) not in pos: continue #判断为回收站后 count = 0 #四个角的垃圾点数 #计算回收站的得分,即四个角的垃圾点数 if (x+1,y+1) in pos: count += 1 if (x+1,y-1) in pos: count += 1 if (x-1,y+1) in pos: count += 1 if (x-1,y-1) in pos: count += 1 score[count] += 1 print(score[0]) print(score[1]) print(score[2]) print(score[3]) print(score[4])
    Processed: 0.018, SQL: 8