全国绿色计算大赛 模拟赛第二阶段 (Python)

第1关气温预测

def dailyTemps(temp_list):
    result = []

    for ca in range(0, len(temp_list)-1):
        for i in range(ca + 1, len(temp_list)):
            ind = temp_list[ca]
            flag = 0
            if (temp_list[i] > ind):
                result.append(i - ca)
                flag = 1
                break
        if (flag == 0):
            result.append(0)

    result.append(0)
    return result


if __name__ == '__main__':
    temp_list = [1,3,2]
    print(dailyTemps(temp_list))

第2关折纸小游戏

def paperFolding(paper_info):
    length = paper_info[0]
    width = paper_info[1]

    result = []

    edge = hcf(length, width)
    result.append(edge)
    result.append(int(length*width/(edge*edge)))

    return result


def hcf(x, y):
    if x > y:
        smaller = y
    else:
        smaller = x
    for i in range(1, smaller + 1):
        if ((x % i == 0) and (y % i == 0)):
            hcf = i
    return hcf


if __name__ == '__main__':
    paper_info = [7, 3]
    print(paperFolding(paper_info))

第3关渡口与船

def countOfShips(ferry):
    result = 0
    mp = [([0] * len(ferry[0])) for p in range(len(ferry))]
    for i in range(len(ferry)):
        for j in range(len(ferry[0])):
            if (ferry[i][j] == "+"):

                result += 1
                if (judge(i + 1, j, ferry) or (judge(i - 1, j, ferry)) or (judge(i, j + 1, ferry)) or (judge(i, j - 1, ferry))):

                    if (judges(i + 1, j, mp) or (judges(i - 1, j, mp)) or (judges(i, j + 1, mp)) or (judges(i, j - 1, mp))):
                        result -= 1
                mp[i][j] = 1
    return result

def judge(x, y, ferry):
    l = len(ferry)
    w = len(ferry[0])
    if (x < 0 or x >= l or y < 0 or y >= w): return False
    if (ferry[x][y] == "+"):
        return True;
    else:
        return False;


def judges(x, y, mp):
    l = len(mp)
    w = len(mp[0])
    if (x < 0 or x >= l or y < 0 or y >= w): return False
    if (mp[x][y] == 1):
        return True;
    else:
        return False;


if __name__ == '__main__':
    ferry = [
        ["+", "o", "o", "+", "o"],
        ["o", "o", "o", "o", "+"],
        ["o", "+", "o", "o", "+"],
        ["o", "+", "o", "o", "+"]
    ]
    print(countOfShips(ferry))

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

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