Created
March 14, 2026 09:39
-
-
Save thinkphp/81daae68504eb660f26e48d06668cc04 to your computer and use it in GitHub Desktop.
divide-et-impera.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| int v[100]; | |
| // 1 5 | |
| /* | |
| n=5 | |
| 10 101 90 23 44 | |
| 1 2 3 5 | |
| 1 5 | |
| return v[1] | |
| return v[2] | |
| return v[3] | |
| return v[4] | |
| return v[5] | |
| */ | |
| int max(int i, int j) { | |
| int a, b; | |
| if(i == j) return v[ i ]; | |
| else { | |
| a = max(i, (i + j) / 2);//max(1,2)//max(1,1) | |
| b = max( (i + j) / 2 + 1, j); //max(3,5)//max(2,2) | |
| if(a > b) return a; | |
| else | |
| return b; | |
| } | |
| } | |
| /* | |
| n=5 | |
| 10 101 90 23 44 | |
| [ind1,ind2,ind3,ind4,ind5] | |
| limita inferioara = 1 | |
| limita superioara = 5 | |
| [1,5] | |
| [ 1, 2 ] [ 3, 5 ] | |
| [1,1] [2,2] [3,4] [4,5] | |
| v[1] v[2] [3,3] [4,4] [4,4][5,5] | |
| v[3] v[4] v[4] v[5] | |
| max(m0,m1) max(m3,m4) max(m4,m5) | |
| */ | |
| int main() { | |
| int n; | |
| cout<<"n="; | |
| cin>>n; | |
| for(int i = 1; i <= n; ++i) cin>>v[ i ]; | |
| cout<<"MAX = "<<max( 1, n ); | |
| return 0; | |
| } | |
| /* | |
| daca i == i valoarea maxima va fi v[i] | |
| contrar vom imparti vectorul in doi vectori (primul vector va contine componentele de la i | |
| la (i + j) div 2), al doilea vector va contine componentele de la (i+j)div 2 + 1, pana la j, | |
| rezolvam subproblemele (aflam maximul pentru fiecare din ele) iar solutia va fi data de valoarea maxima dintre rezultatele celor doua subprobleme. | |
| problema | |
| subprob1 subproblema2 | |
| subprobl1,1 subprobl2,1 | |
| int factorial(int n) { | |
| if(n == 1) return 1 | |
| else return n * factorial(n-1) | |
| } | |
| factorial(5) | |
| n! = 1 * 2 * ... * n; | |
| 5! = 1 * 2 * 3 * 4 * 5 | |
| factorial(5) = 5 * factorial(4) = 5 * 4 * factorial(3) = 5 * 4 * 3 * factorial(2) = 5 * 4 * 3 * 2 * 1 * 1 | |
| 1 \/ | |
| 1 1*1 | |
| 2 1 * 2 | |
| 3 2 * 3 | |
| 4 6 * 4 | |
| 5 /|\ 24*5 | |
| | | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment