#include <iostream>
#include "maklib.h"
#include "message.h"
using namespace std;
int sequence()
{
// Initialize variables here
int max_so_far = ElementAt(1), max_ending_here = ElementAt(1);
// OPTIONAL: These variables can be added in to track the position of the subarray
// size_t begin = 0;
// size_t begin_temp = 0;
// size_t end = 0;
// Find sequence by looping through
for(size_t i = 2; i < Size()+1; i++)
{
// calculate max_ending_here
if(max_ending_here < 0)
{
max_ending_here = ElementAt(i);
// begin_temp = i;
}
else
{
max_ending_here += ElementAt(i);
}
// calculate max_so_far
if(max_ending_here >= max_so_far )
{
max_so_far = max_ending_here;
// begin = begin_temp;
// end = i;
}
}
return max_so_far ;
}
int main() {
if (MyNodeId() == 0) {
cout << sequence() << endl;
} else {
sequence();
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <iostream> #include "maklib.h" #include "message.h" using namespace std; int sequence() { // Initialize variables here int max_so_far = ElementAt(1), max_ending_here = ElementAt(1); // OPTIONAL: These variables can be added in to track the position of the subarray // size_t begin = 0; // size_t begin_temp = 0; // size_t end = 0; // Find sequence by looping through for(size_t i = 2; i < Size()+1; i++) { // calculate max_ending_here if(max_ending_here < 0) { max_ending_here = ElementAt(i); // begin_temp = i; } else { max_ending_here += ElementAt(i); } // calculate max_so_far if(max_ending_here >= max_so_far ) { max_so_far = max_ending_here; // begin = begin_temp; // end = i; } } return max_so_far ; } int main() { if (MyNodeId() == 0) { cout << sequence() << endl; } else { sequence(); } return 0; } |
English