(If you want to learn, read the comments from the code ;) )
You could also try using linked lists in C++. You can create a struct like this:
struct MyNumber{
//The number itself
int me;
//Each number has 2 derived numbers
MyNumber *childA,*childB;
//A default constructor of a number that doesn't have 'sons'
MyNumber(int me):me(me),childA(NULL),childB(NULL)
{}
};
Then you create a "MyNumber" that will be the top of the pyramid, and a list of "MyNumber" that will represent the bottom of the pyramid:
#include <iostream>
#include <vector>
using namespace std;
//The top of the pyramid only has 1 number. It's null because it hasn't been initiated.
MyNumber *top=NULL;
//The bottom is composed of a list of numbers
vector<MyNumber*> bottom;
After that you create a function wich adds new levels to the pyramid:
void add_level(int count,int * list_of_numbers){
//if the top of the pyramid doesn't exist (is null) then initiate one
if(top==NULL)
{
//You can only add 1 number to the top. If not, there must be an error.
if(count!=1){
cout<<"Error: can't add more than one number at the top of the pyramid"<<endl;
return;
}
//You made it correctly! We will create the top with the first number of the list.
top=new Number(list_of_numbers[0]);
}
//The top is already created. We will connect numbers.
else{
//The new level to be added:
vector<MyNumber*> new_level;
//The count of the new numbers list must be 1 more than the bottom size,
//unless that the bottom size is 0: the count will be 2
if(bottom.size()==0)
if(count!=2){
cout<<"Error: the second level can only have 2 numbers"<<endl;
return;
}
else if( bottom.size()!=(count-1) ){
cout<<"Error: the new level can only have "<<bottom.size()+1<<" numbers"<<endl;
return;
}
//adding the numbers to the new level list
bool bfirst_time=true;
for(int i=0,e=0;i<count;i++)
{
MyNumber * new_number=new MyNumber(list_of_numbers[i]);
new_level.push_back(new_number);
if(bfirst_time)
{
//Setting the childA from the old bottom as the first number from the list
//Do this only 1 time
bottom[0]->childA=new_number;
bfirst_time=false;
}
else{
//The 'e' bottom number will be new number parent as well as the 'e+1'
//bottom number (every number has 2 parents except the first and last
//number from the new list)
bottom[e]->childB=new_number;
e++;
if(e<bottom.size())
bottom[e]->childA=new_number;
}
}
//connecting those numbers with their parent/s(the old bottom of the pyramid)
}
}
Next you add the numbers to the pyramid using the function:
int * list_of_numbers=new int[1];
list_of_numbers[0]=1;
//adding the top
add_level(1,list_of_numbers);
list_of_numbers=new int[2];
list_of_numbers[0]=2;
list_of_numbers[0]=3;
//adding the second level
add_level(2,list_of_numbers);
...
Finally you can get the maximum sum by this way:
#include <iostream>
#include <algorithm>
using namespace std;
//the int containing the sum
int max_sum=top->me;
//a clone of the top
MyNumber * clone=top;
//same as "while you don't reach the bottom of the pyramid, continue"
while(clone->childA!=NULL && clone->childB!=NULL)
{
//add the biggest value to the max_sum variable
max_sum+=max(clone->childA->me,clone->childB->me);
//setting the clone as the biggest child
clone=(clone->childA->me > clone->childB->me)? clone->childA:clone->childB;
}
You can improve this code a lot. Probably it is easier to make it in C# but I don't use it :(
There may be some errors in the code, I didn't test it :P