全国高校绿色计算大赛 预赛第二阶段(Python)

第1关统计分数的麻烦

class Task:
    def get_lost_scores(self, scores):
        s = ""
        index = [1 for i in range(10001)]
        for i in scores:
            index[i] = 0
        for i in range(1,10001):
            if(index[i] == 1):
                s+=str(i)
        ans = int(s) % 11
        return ans




if __name__ == '__main__':
    task = Task
    score = [2, 3, 5, 6, 7, 8, 9]
    print(task.get_lost_scores(task, score))

第2关最强战队

#!/usr/bin/env python
# -*- coding: utf-8 -*-
class BestTeam:
    def get_best_team(self, numbers, abilities, selectedNum, distance):
        n = numbers
        k = selectedNum
        d = distance
        res = 0
        maxval = [([0] * 100) for i in range(50)]
        minval = [([0] * 100) for i in range(50)]
        for i in range(n):
            minval[i][0] = abilities[i]
            maxval[i][0] = abilities[i]
        for i in range(n):
            for j in range(1, k):
                # for kk in range(i - 1, 0, -1):
                kk = i - 1
                while (kk >= max(i - d, 0)):
                    # print(kk)
                    maxval[i][j] = max(maxval[i][j],
                                       max(maxval[kk][j - 1] * abilities[i], minval[kk][j - 1] * abilities[i]));
                    minval[i][j] = min(minval[i][j],
                                       min(maxval[kk][j - 1] * abilities[i], minval[kk][j - 1] * abilities[i]));
                    kk -= 1
            res = max(res, max(maxval[i][k - 1], minval[i][k - 1]));
        return res


if __name__ == '__main__':
    bestTeam = BestTeam
    numbers = 10
    abilities = [12,11,8,9,15,22,7,9,12,10]
    selectedNum = 3
    distance = 5
    print(bestTeam.get_best_team(bestTeam, numbers, abilities, selectedNum, distance))

第3关完美的团建活动

#!/usr/bin/env python
# -*- coding: utf-8 -*-
class TeamBuilding:

    def get_minium_steps(self, stones):
        xx = 0
        yy = 0
        ans = 0
        n = len(stones)
        points = []
        for i in range(n):
            for j in range(n):
                if stones[i][j] > 1:
                    points.append((stones[i][j], i, j))
        points = sorted(points, key=lambda points: points[0]);
        for nn, x, y in points:
            mp = [([0] * n) for p in range(n)]

            ind = self.bfs(self, xx, yy, x, y, stones, mp, 0)
            # print(ind)
            if ind != -1:
                ans += ind
            else:
                return -1
            xx = x
            yy = y
        return ans

    def bfs(self, xx, yy, x, y, stones, mp, step):
        if(xx == x and yy == y): return 0
        mp[xx][yy] = 1
        to = [[0, 1], [1, 0], [-1, 0], [0, -1]]
        import queue
        q = queue.Queue()
        q.put((xx, yy, step))
        while not q.empty():
            tx, ty, stepp = q.get()
            for i in range(4):
                tox = tx + to[i][0]
                toy = ty + to[i][1]
                if self.judge(self, tox, toy, stones) and mp[tox][toy] != 1:
                    if tox == x and toy == y:
                        return stepp + 1
                    q.put((tox, toy, stepp + 1))
                    mp[tox][toy] = 1
        return -1

    def judge(self, x, y, stones):
        l = len(stones)
        w = len(stones[0])
        if x < 0 or x >= l or y < 0 or y >= w:
            return False
        if stones[x][y] == 0:
            return False
        else:
            return True


if __name__ == '__main__':
    t = TeamBuilding
    stones = [[12,34,5,7,8,0],
    [1,0,8,9,12,0],
    [13,0,0,0,11,24],
    [23,32,17,0,0,10],
    [1,2,3,0,0,6],
    [4,8,12,0,0,19]]


    # stones = [[2, 3, 4], [0, 0, 5], [8, 7, 6]]
    print(t.get_minium_steps(t, stones))
    # mp = [([0] * 3) for p in range(3)]
    # print(t.bfs(t, 0, 0, 2, 0, stones,mp, 0))

转载于:https://www.cnblogs.com/somliy/p/9868998.html

你可能感兴趣的:(全国高校绿色计算大赛 预赛第二阶段(Python))