Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Soma Acumulada 1D

a guest Apr 3rd, 2020 249 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // SOMA ACUMULADA 1D (VETOR)
  3. // Complexidade:
  4. // • Criação do vetor: O(N)
  5. // • Soma do intervalo: O(1)
  6.  
  7.  
  8. #include <iostream>
  9. using namespace std;
  10. int main()
  11. {
  12.     int n;
  13.     int s = 0;
  14.     cin >> n;
  15.     int vt[n], soma[n];
  16.  
  17.     //leitura da matriz
  18.     for(int i = 0; i < n; i++)
  19.     {
  20.         cin >> vt[i];
  21.         s += vt[i];
  22.         soma[i] = s;
  23.     }
  24.  
  25.     int a, b; //intervalo que se deseja somar
  26.     int q; //número de intervalos
  27.     cin >> q;
  28.     for(int i = 0; i < q; i++)
  29.     {
  30.         cin >> a >> b;
  31.         if(a == 0)
  32.             cout << soma[b] << endl;
  33.         else
  34.             cout << soma[b] - soma[a-1] << endl;
  35.     }
  36.     return 0;
  37. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top