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
2220 - DifMax
(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!
== Explicație rezolvare == Programul de mai sus conține două funcții, funcția ''' afis_diferenta_maxima(n, a)''' și funcția '''citire_conform_restrictiilor(n, a)''', care se vor rula în interiorul main-ului ('''if __name__ == '__main__' ''', linia 26) după citirea numărului n (linia 27) și celor n numere pe care le vom pune în șirul „a” (linia 28).<br> După ce am citit elementele, se va apela '''funcția citire_conform_restrictiilor(n, a)''' care primește doi parametri: vectorul „a” și lungimea sa, reprezentată de variabila „n”. Funcția verifică dacă lungimea vectorului n este între 1 și 100000 (linia 13), dacă elementele vectorului sunt cuprinse între -1000000000 și 1000000000 (liniile 16, 17) și dacă n este lungimea vectorului „a” (linia 20). Dacă oricare dintre condiții este încălcată, se va afișa mesajul „Datele nu corespund restricțiilor impuse.” și se va ieși din program cu comanda exit(). Dacă toate condițiile sunt respectate, se va afișa mesajul „Datele sunt introduse corect.” (linia 23) și se va continua programul.<br> Dacă s-au introdus corect datele, se va apela funcția '''afis_diferenta_maxima(n, a)''' care primește ca parametrii vectorul „a” și dimensiunea sa „n”. În această funcție se inițializează variabila diferenta_maxima cu -10000000000 (valoarea cât mai mică pentru a fi înlocuită pentru orice valoare din șirul a) și se folosesc două bucle for pentru a parcurge toate combinațiile posibile de două numere din șir (liniile 5, 6, în a doua buclă pornim de la i+1 deoarece în problemă se spune că indicii i, j au proprietatea că i < j). În interiorul buclei for, dacă diferența dintre cele două numere este mai mare decât diferenta_maxima (un număr inițial foarte mic), atunci diferenta_maxima este actualizată cu noua valoare. La sfârșitul funcției, după parcurgerea tuturor combinațiilor, în variabila diferenta_maxima vom avea valoarea maximă a diferențelor, care va fi afișată pe ecran utilizând funcția print.
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