Advertisement
STANAANDREY

sum dei

Oct 13th, 2020 (edited)
1,927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. /*sa se scrie un program ce foloseste metoda DEI pt a calcula suma dintr-un vector*/
  2. #include <iostream>
  3. #define NMAX 1000
  4. using namespace std;
  5. int n, v[NMAX];
  6.  
  7. void read() {
  8.     cin >> n;
  9.     for (int i = 0; i < n; i++)
  10.         cin >> v[i];
  11. }
  12.  
  13. int getsum(int p, int q) {
  14.     if (p == q)
  15.         return v[p];
  16.     int mid = (p + q) / 2;
  17.     return getsum(p, mid) + getsum(mid + 1, q);
  18. }
  19.  
  20. int main() {
  21.     read();
  22.     cout << getsum(0, n - 1);
  23.     return 0;
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement