The document presents a recursive algorithm called "divide and conquer" to calculate the sum of elements in an array or the greatest common divisor (GCD) of elements in an array. The algorithm divides the problem into subproblems of the same type, solves the subproblems recursively, and combines the results. It first checks if the problem can be solved directly, otherwise it divides the range in half, calls itself on the halves, and combines the results. Pseudocode is provided to implement the algorithm to calculate the sum or GCD. The program takes input of array elements, calls the divide and conquer algorithm, and outputs the result.
The document presents a recursive algorithm called "divide and conquer" to calculate the sum of elements in an array or the greatest common divisor (GCD) of elements in an array. The algorithm divides the problem into subproblems of the same type, solves the subproblems recursively, and combines the results. It first checks if the problem can be solved directly, otherwise it divides the range in half, calls itself on the halves, and combines the results. Pseudocode is provided to implement the algorithm to calculate the sum or GCD. The program takes input of array elements, calls the divide and conquer algorithm, and outputs the result.
The document presents a recursive algorithm called "divide and conquer" to calculate the sum of elements in an array or the greatest common divisor (GCD) of elements in an array. The algorithm divides the problem into subproblems of the same type, solves the subproblems recursively, and combines the results. It first checks if the problem can be solved directly, otherwise it divides the range in half, calls itself on the halves, and combines the results. Pseudocode is provided to implement the algorithm to calculate the sum or GCD. The program takes input of array elements, calls the divide and conquer algorithm, and outputs the result.
procedure DesparteSiStapineste(i, j : integer; var s : real);
Program pr3pag138; var m : integer; { Calcularea sumei prin metoda desparte şi stăpîneşte } s1, s2 : real; const nmax=100; begin var A : array[1..nmax] of real; if SolutieDirecta(i, j) then Prelucrare(i, j, s) i, n : 1..nmax; else s : real; begin function SolutieDirecta(i, j : integer) : boolean; m:=(j-i) div 2; begin DesparteSiStapineste(i, i+m, s1); SolutieDirecta:=false; DesparteSiStapineste(i+m+1, j, s2); if (j-i<1) then SolutieDirecta:=true; Combina(s1, s2, s); end; { SolutieDirecta } end; procedure Prelucrare(i, j : integer; var s : real); end; { DesparteSiStapineste } begin begin if i=j then s:=A[i]; write('Daţi n:'); readln(n); end; { Prelucrare } writeln('Daţi ', n, ' numere reale'); procedure Combina(s1, s2 : real; var s : real); for i:=1 to n do read(A[i]); begin writeln; s:=s1+s2; end; { Combina } DesparteSiStapineste(1, n, s); writeln('Suma: ', s); readln; readln; end. Exemplul 2
procedure DesparteSiStapineste(i, j : integer; var cmmdc : integer);
var m : integer; Program pr8pag139; s1, s2 : real; { Calcularea celui mai mare divizor comun prin metoda desparte şi stăpîneşte } begin const nmax=100; if SolutieDirecta(i, j) then Prelucrare(i, j, cmmdc) var A : array[1..nmax] of integer; else i, n : 1..nmax; begin cmmdc, cmmdc1, cmmdc2 : integer; m:=(j-i) div 2; function SolutieDirecta(i, j : integer) : boolean; DesparteSiStapineste(i, i+m, cmmdc1); begin DesparteSiStapineste(i+m+1, j, cmmdc2); SolutieDirecta:=false; Combina(cmmdc1, cmmdc2, cmmdc); if (j-i<2) then SolutieDirecta:=true; end; end; { SolutieDirecta } end; { DesparteSiStapineste } procedure Prelucrare(i, j : integer; var cmmdc : integer); begin begin write('Daţi n: '); readln(n); while A[i]<>A[j] do if A[i]>A[j] then A[i]:=A[i]-A[j] else A[j]:=A[j]-A[i]; cmmdc:=A[i]; writeln('Daţi ', n, ' numere naturale'); end; { Prelucrare } for i:=1 to n do read(A[i]); procedure Combina(cmmdc1, cmmdc2 : integer; var cmmdc : integer); writeln; begin DesparteSiStapineste(1, n, cmmdc); cmmdc:=cmmdc1; writeln('Cel mai mare divizor comun: ', cmmdc); if cmmdc1>cmmdc2 then cmmdc:=cmmdc2; readln; end; { Combina } readln; end.