SHARE
TWEET

Untitled

a guest Feb 27th, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Sum.h"
  2. #include <iostream>
  3. #include <chrono>
  4.  
  5. string calcSum(int* sequence, int size) { //somatorio,indice;
  6.  
  7.     if (size <= 0) {
  8.         return "";
  9.     }
  10.     else if (size == 1) {
  11.         return to_string(sequence[0]) + ",0" + ";";
  12.     }
  13.  
  14.     string result = "";
  15.     int n = 0;
  16.  
  17.  
  18.     while(n != size){
  19.         pair<int, int> temp = sumMin(sequence, n+1, size);
  20.         n++;
  21.         result += to_string(temp.first) + "," + to_string(temp.second) + ";";
  22.     }
  23.  
  24.     return result;
  25. }
  26.  
  27. pair<int, int> sumMin(int* sequence, int n, int size){
  28.  
  29.     int index = 0, sum = 0, sumtemp = 0;
  30.  
  31.     for(int i = 0; i < size; i++){
  32.  
  33.         if((i+n) <= size){
  34.             sumtemp = 0;
  35.  
  36.             for(int j = i; j < i + n; j++){
  37.                 sumtemp += sequence[j];
  38.             }
  39.  
  40.             if (sumtemp < sum || (i == 0)){
  41.                 index = i;
  42.                 sum = sumtemp;
  43.             }
  44.         }
  45.         else
  46.             break;
  47.  
  48.     }
  49.  
  50.     return make_pair(sum, index);
  51. }
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