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
1060 - Porumb
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
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!
== Rezolvare == <syntaxhighlight lang="python" line> #1060 def porumbi_primul_agricultor(n): return (n + 1) // 2 def numar_agricultori(n): return (n + 1) // 2 def trecere_porumb_cautat(n, x): agri = numar_agricultori(n) pozitie_x = (x - 1) % n + 1 trecere_x = (pozitie_x - 1) // agri + 1 return trecere_x def validate_input(n, x): if not isinstance(n, int) or not isinstance(x, int): print("Datele nu corespund restricțiilor impuse.") return False elif n < x: print("Datele nu corespund restricțiilor impuse.") return False elif n < 1 or n > 1000000000 or x < 1 or x > 1000000000: print("Datele nu corespund restricțiilor impuse.") return False else: print("Datele sunt introduse corect.") return True if __name__ == '__main__': n = int(input("Introduceti numarul de porumbi: ")) x = int(input("Introduceti numarul porumbului cautat: ")) if validate_input(n, x): primul_agri = porumbi_primul_agricultor(n) agri = numar_agricultori(n) ultimul_porumb = n - (n // agri) * (agri - 1) trecere_x = trecere_porumb_cautat(n, x) print("Numarul de porumbi culesi de primul agri: ", primul_agri) print("Numarul de agri care culeg sirul de n porumbi: ", agri) print("Porumbul cu numarul ", x, "este cules la trecerea ", trecere_x) print("Numarul ultimului porumb cules: ", ultimul_porumb) </syntaxhighlight> '''Explicația codului:''' Acesta este un program care calculează informații despre culegerea porumbului de către agricultori, date fiind numărul total de porumbi și poziția porumbului căutat. Funcția `porumbi_primul_agricultor(n)` returnează numărul de porumbi culeși de primul agricultor, calculat prin împărțirea numărului total de porumbi la doi și apoi rotunjit în sus cu ajutorul operatorului de divizare întreagă `//`. Funcția `numar_agricultori(n)` returnează numărul de agricultori care culeg porumbul, calculat în același mod ca mai sus. Funcția `trecere_porumb_cautat(n, x)` calculează numărul de treceri ale șirului de agricultori necesare pentru a ajunge la poziția porumbului căutat. Variabila `agri` este numărul de agricultori calculat anterior, iar variabila `pozitie_x` reprezintă poziția porumbului căutat pe șirul circular de porumbi. Variabila `trecere_x` reprezintă numărul de treceri necesare pentru ca porumbul cu numărul `x` să fie cules de unul dintre agricultori. Funcția `validate_input(n, x)` verifică dacă numerele introduse de utilizator sunt întregi și îndeplinesc alte restricții impuse. În caz contrar, afișează un mesaj de eroare și returnează `False`. Dacă numerele sunt valide, afișează un mesaj de confirmare și returnează `True`. În funcția principală `main`, utilizatorul este întâi invitat să introducă numărul total de porumbi și poziția porumbului căutat. Apoi se verifică dacă acestea sunt valori valide cu ajutorul funcției `validate_input`. Dacă valorile sunt valide, se calculează informațiile cerute și se afișează.
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