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
4068 - Grade K
(section)
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!
= Exemplul 2 = <code>gradekIN.txt</code> 5 101 1 4 1 3 3 5 4 5 2 4 1 2 4 2 3 4 <code>gradekOUT.txt</code> Datele nu corespund restrictiilor impuse == Rezolvare == <syntaxhighlight lang="python3" line="1"> def verifica_restrictii(n, k, muchii): # Verificăm restricțiile impuse if not (1 <= n <= 100): return False if not (1 <= k <= n): return False for i, j in muchii: if not (1 <= i <= n) or not (1 <= j <= n): return False return True def noduri_cu_grad_egalcu_k(n, k, muchii): graf = {} # Inițializăm dicționarul grafului for i in range(1, n+1): graf[i] = set() # Adăugăm muchiile în graf for muchie in muchii: i, j = muchie graf[i].add(j) graf[j].add(i) # Calculăm gradul nodului k grad_k = len(graf[k]) # Determinăm nodurile cu gradul egal cu gradul nodului k noduri_egale = [nod for nod, vecini in graf.items() if len(vecini) == grad_k] return noduri_egale # Citim datele de intrare try: with open("gradekIN.txt", "r") as f: n, k = map(int, f.readline().split()) muchii = [tuple(map(int, line.split())) for line in f.readlines()] # Verificăm restricțiile if not verifica_restrictii(n, k, muchii): raise ValueError # Determinăm nodurile cu gradul egal cu gradul nodului k noduri_rezultat = noduri_cu_grad_egalcu_k(n, k, muchii) # Scriem rezultatul în fișierul de ieșire with open("gradekOUT.txt", "w") as f: if noduri_rezultat: f.write(str(len(noduri_rezultat)) + "\n") f.write(" ".join(map(str, noduri_rezultat))) else: f.write("Nu există noduri cu gradul egal cu gradul nodului k") except ValueError: with open("gradekOUT.txt", "w") as f: f.write("Datele nu corespund restrictiilor impuse") except Exception as e: with open("gradekOUT.txt", "w") as f: f.write("A apărut o eroare în timpul procesării datelor.\n") f.write(str(e)) </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