4. Całe zadanie: prostokąty w wyniki4.txt

Matura próbna grudzień 2024 - Matura z informatyki: arkusze krok po kroku (Python)

Pełne rozwiązanie zadań 4.1-4.3 jednym programem na pliku `prostokaty.txt`.

rectangles = []
with open("prostokaty.txt") as f:
    for line in f:
        h, s = line.split()  # wysokość, szerokość
        rectangles.append((int(h), int(s)))

# 4.1 pola prostokątów
areas = [h * s for h, s in rectangles]

# 4.2 prostokąt mieści się w poprzednim, gdy żaden wymiar nie jest większy
best, length, last = 1, 1, rectangles[0]
for i in range(1, len(rectangles)):
    h, s = rectangles[i]
    ph, ps = rectangles[i - 1]
    if h <= ph and s <= ps:
        length = length + 1  # ciąg trwa
    else:
        length = 1  # nowy ciąg zaczyna się od tego prostokąta
    if length > best:
        best, last = length, rectangles[i]

# 4.3 sklejać można tylko prostokąty o tej samej wysokości: szerokości grupowane po wysokości
widths = {}
for h, s in rectangles:
    if h not in widths:
        widths[h] = []
    widths[h].append(s)
glued = {}
for k in (2, 3, 5):
    glued[k] = 0
    for h, row in widths.items():
        if len(row) >= k:
            top = sum(sorted(row, reverse=True)[:k])  # k najszerszych tej wysokości
            glued[k] = max(glued[k], top)

with open("wyniki4.txt", "w", encoding="utf-8") as out:
    print("4.1", min(areas), max(areas), file=out)
    print("4.2", best, *last, file=out)
    print("4.3", file=out)
    for k in (2, 3, 5):
        print(k, glued[k], file=out)

print(open("wyniki4.txt", encoding="utf-8").read())

Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.

Wszystkie lekcje kursu Matura z informatyki: arkusze krok po kroku·Zaloguj się, aby zapisać postęp na koncie.