2709 - Excursie2

De la Universitas MediaWiki

Sursa: [1]

Cerinţa

Se organizează o excursie cu n participanți cu numere de ordine unice de la 1 la n pentru care se folosesc 3 mijloace de transport. Să se determine dacă se pot aranja participanții astfel încât suma numerelor de ordine din cele trei mașini să fie aceeași. Dacă este posibilă aranjarea, se vor afișa pe 3 linii numerele de ordine separate prin spații, numerele dintr-o mașină pe câte un rând, altfel se afișează NU.

Date de intrare

Programul citește de la tastatură numărul n.

Date de ieșire

Programul va afișa pe ecran, mesajul "Datele introduse corespund cerințelor" și pe o linie nouă se vor afișa pe 3 linii numerele de ordine separate prin spații, numerele dintr-o mașină pe câte un rând. În caz contrar programul va afișa pe o linie noua mesajul NU.

Restricţii şi precizări

  • 1 ⩽ n ⩽ 1001
  • ordinea afișării nu contează atât timp cât ea este corectă.

Exemplul 1

Intrare
Introduceti numar: 7
Ieșire
NU


Exemplul 2

Intrare
Introduceti numar: 5
Ieșire
5
2 3
1 4


Rezolvare

#2709
def print_first_sequence(s):
    if s == 10:
        print("1 2 3 4 5 ", end="")
    elif s == 9:
        print("1 2 3 6 ", end="")
    elif s == 7:
        print("3 4 ", end="")
    elif s == 6:
        print("5 ", end="")


def print_second_sequence(s):
    if s == 10:
        print("7 8 ", end="")
    elif s == 9:
        print("5 7 ", end="")
    elif s == 7:
        print("2 5 ", end="")
    elif s == 6:
        print("2 3 ", end="")


def print_third_sequence(s):
    if s == 10:
        print("6 9 ", end="")
    elif s == 9:
        print("4 8 ", end="")
    elif s == 7:
        print("1 6 ", end="")
    elif s == 6:
        print("1 4 ", end="")


def validate_input(n):
    if n < 1 or n > 1001 or (n < 5 or n * (n + 1) % 3):
        print("NU")
        return False
    return True


def main():
    n = int(input("Introduceti numar: "))
    if not validate_input(n):
        return

    if (n - 9) % 6 == 0:
        s, x = 10, (n - 9) // 6
    elif (n - 8) % 6 == 0:
        s, x = 9, (n - 8) // 6
    elif (n - 6) % 6 == 0:
        s, x = 7, (n - 6) // 6
    elif (n - 5) % 6 == 0:
        s, x = 6, (n - 5) // 6

    print_first_sequence(s)
    for i in range(x):
        print(s + 6 * i, s + 5 + 6 * i, end=" ")
    print()

    print_second_sequence(s)
    for i in range(x):
        print(s + 1 + 6 * i, s + 4 + 6 * i, end=" ")
    print()

    print_third_sequence(s)
    for i in range(x):
        print(s + 2 + 6 * i, s + 3 + 6 * i, end=" ")

    print("\nDatele introduse corespund cerintelor.")
    print()

main()

Explicatie rezolvare