Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Bitnami MediaWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
1398 - Determinant
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= Cerința = Se dă o matrice pătratică de dimensiune <code>n</code>. Să se calculeze determinantul ei. = Date de intrare = Fișierul de intrare <code>determinantIN.txt</code> conține pe prima linie numărul <code>n</code>, iar pe următoarele <code>n</code> linii câte <code>n</code> numere întregi, reprezentând elementele matricei. = Date de ieșire = Fișierul de ieșire <code>determinantOUT.txt</code> va conține pe prima linie numărul <code>D</code>, reprezentând valoarea determinantului. = Restricții și precizări = * <code>1 ≤ n ≤ 10</code> = Exemplul 1 = <code>determinantIN.txt</code> 3 1 -2 3 0 1 -1 2 1 -3 <code>determinantOUT.txt</code> -4 = Exemplul 2 = <code>determinantIN.txt</code> 20 1 -2 3 0 1 -1 2 1 -3 <code>determinantOUT.txt</code> Datele de intrare nu respectas restrictiile impuse. == Rezolvare == <syntaxhighlight lang="python3" line="1"> def are_restricții_valide(n, matrice): if not (1 <= n <= 10): return False for linie in matrice: if len(linie) != n: return False return True def calcul_cofactor(matrice, linie, coloana): submatrice = [row[:coloana] + row[coloana + 1:] for row in (matrice[:linie] + matrice[linie + 1:])] semn = (-1) ** (linie + coloana) return semn * calcul_determinant(submatrice) def calcul_determinant(matrice): n = len(matrice) if n == 1: return matrice[0][0] elif n == 2: return matrice[0][0] * matrice[1][1] - matrice[0][1] * matrice[1][0] else: determinant = 0 for coloana in range(n): determinant += matrice[0][coloana] * calcul_cofactor(matrice, 0, coloana) return determinant # Citirea datelor de intrare try: with open("determinantIN.txt", "r") as file_in: n = int(file_in.readline().strip()) matrice = [list(map(int, file_in.readline().split())) for _ in range(n)] except FileNotFoundError: with open("determinantOUT.txt", "w") as file_out: file_out.write("Fișierul determinantIN.txt nu a fost găsit.") exit() except Exception as e: with open("determinantOUT.txt", "w") as file_out: file_out.write(f"Eroare la citirea datelor de intrare: {str(e)}") exit() # Verificarea restricțiilor if not are_restricții_valide(n, matrice): with open("determinantOUT.txt", "w") as file_out: file_out.write("Datele de intrare nu respecta restrictiile impuse.") exit() # Calculul determinantului determinant = calcul_determinant(matrice) # Scrierea rezultatului în fișierul de ieșire with open("determinantOUT.txt", "w") as file_out: file_out.write(str(determinant)) </syntaxhighlight>
Summary:
Please note that all contributions to Bitnami MediaWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Bitnami MediaWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width