1 / 7

Divide et Impera

Divide et Impera. De Cacior Mihai. Divide et impera provine din latina , inseamna divide si cucereste .

xenos
Download Presentation

Divide et Impera

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Divide et Impera De CaciorMihai Divide et imperaprovine din latina, inseamna divide sicucereste. Divide et impera se bazează pe principiul descompunerii problemei în două sau mai multe subprobleme, care se rezolvă, iar soluția pentru problema inițială se obține combinând soluțiile subproblemelor.

  2. #include<iostream> using namespace std; int v[20]; intsuma(inta,int z) {int m,d1,d2; if(a!=z) {m=(a+z)/2; d1=suma(a,m); d2=suma(m+1,z); return d1+d2;//pentrusuma return d1*d2;//pentruprodus } else return v[a]; } int main() { int n; cout<<"n="; cin>>n; for(int i=1;i<=n;i++) { cout<<"v["<<i<<"]="; cin>>v[i]; } cout<<"sumaeste "<<suma(1,n); } Suma/Produluselementelorunui vector Metodapresupunedescompunereavectorului v in subvectorisiacestiasuntimpartiti la randullorpanacandfiecare vector are un element. Elementelevectorilorsuntadunatisauinmultitipe rand doicatedoipanarezultasolutiafinala.

  3. Elementul maxim sau minim dintr-un vector #include<iostream> using namespace std; int v[20]; intmaxmin(int a, int z) { intx,y,m; m=(a+z)/2; if (a<z) //pentru maxim if(a>z) //pentru minim { x = maxmin(a,m); y = maxmin(m+1,z); if (x<y) return y; else return x; } else return v[a]; } int main() { int n; cout<<"n="; cin>>n; for(int i=1;i<=n;i++) { cout<<"v["<<i<<"]="; cin>>v[i]; } cout<<"max/min este "<<maxmin(1,n); } Cmmdcsicmmmc-ul a douanumere. #include <iostream> using namespace std; intcmmdc(inta,int b) { int r; r=a%b; while(r!=0) { a=b; b=r; r=a%b; } return b; } intcmmmc(int a, int b) { return(a*b/cmmdc(a,b)); } int main() { intx,y; cout<<"x="; cin>>x; cout<<"y="; cin>>y; cout<<"C.m.m.d.ceste "<<cmmdc(x,y)<<endl; cout<<"C.m.m.m.ceste "<<cmmmc(x,y)<<endl; }

  4. Turnurile din hanoi • Se dau 3 tijesimbolizateprina,b,c. Petija a se gasesc n discuri de diametrediferite, asezate in ordinedescrescatoare a diametrelor. Se ceresa se mute de petija a pe b, utilizandcatijaintermediaratija c, toatecele n discuri. • Rezolvare: • Daca n=1 se face mutarea a-b. • Daca n=2 se facmutarile a-c,a-b,c-b. • Daca n>2 descompunemprogramul in subprograme in care n<2 sirezolvam. #include<iostream> using namespace std; char a,b,c; int n; inthanoi(intn,chara,char b, char c) { if(n==1) cout<<a<<b<<" "; else { hanoi(n-1,a,c,b); cout<<a<<b<<" "; hanoi(n-1,c,b,a); } } int main() { cout<<"n=";cin>>n; hanoi(n,'a','b','c'); }

  5. Cautareabinaraintr-un vector #include<iostream> using namespace std; intn,x,v[10],m; intcaut(ints,int d) { if(s>d) return -99; else { m=(s+d)/2; if(x==v[m]) return m; if(x<v[m]) return caut(s,m-1); else return caut(m+1,d); } } int main() { cout<<"n= ";cin>>n; cout<<"x= ";cin>>x; cout<<"dati "<<n<<" elemente (in ordinecrescatoare)"<<endl; for(int i=1;i<=n;i++) cin>>v[i]; cout<<"elementul "<<x<<" a fostgasitpepozitia: "<<caut(1,n); return 0; } Se cauta un element datintr-un vector cu elementecrescatoare. Cu metoda divide et imperavectorulesteinpartit, elementulcautatestecomparat cu mijloculvectoruluidacaestemai mare se cauta in jumatareavectorului de dupamijloc, iardacaestemaimic se cauta in jumatateadinainteamijlocului. Dacelementul nu estegasit se afiseaza -99.

  6. Sortareaprininterclasare, Mergesort #include<iostream> using namespace std; int a[100],n; intinterclasare(inti,intm,int j) { int b[100]; int x=i; int k=1; int y=m+1; while(x<=m && y<=j) if (a[x]<a[y]) b[k++]=a[x++]; else b[k++]=a[y++]; while (x<=m) b[k++]=a[x++]; while (y<=j) b[k++]=a[y++]; int t=i; for (k=1;k<=(j-i)+1;k++) a[t++]=b[k]; } intdei(inti,int j) { if (i<j) { int m=(i+j)/2; dei(i,m); dei(m+1,j); interclasare(i,m,j); } Vectorul se desparte cu metoda divide et imperapanacandfiecare vector are un singur element. Acestivectorisuntordonatisicompunsolutia. int main() { int i; cout<<"n="; cin>>n; for(int i=1;i<=n;i++) { cout<<"a["<<i<<"]="; cin>>a[i]; } dei(1,n); for(i=1;i<=n;i++) cout<<a[i]<<' '; return 0; }

  7. Metoda Quicksort SortareRapida • Quicksort efectueazasortareabazandu-se pe o strategie divide et impera. Astfel, el impartelista de sortatindouasublistemaiusor de sortat. • Pasiialgoritmuluisunt: • Se alege un element al listeinumit pivot • Se reordoneazalistaastfelincattoateelementalemaimicidacatpivotulsa fie plasateinainteapivotuluisitoateelementalemaimarisa fie dupa pivot. • Se sorteazarecursizsublista de elementemaimicidecatpivotulsisublista de elementemaimaridecatpivotul.

More Related