Triangle problem

I'm trying to solve Euler's 18 problem -> http://projecteuler.net/index.php?section=problems&id=18

I am trying to do this with C ++ (I am retraining it and Euler's problems do for good tutorial / search stuff)

#include <iostream>

using namespace std;

long long unsigned countNums(short,short,short array[][15],short,bool,bool);

int main(int argc,char **argv) {

    long long unsigned max = 0;
    long long unsigned sum;


    short piramide[][15] = {{75,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                            {95,64,0,0,0,0,0,0,0,0,0,0,0,0,0},
                            {17,47,82,0,0,0,0,0,0,0,0,0,0,0,0},
                            {18,35,87,10,0,0,0,0,0,0,0,0,0,0,0},
                            {20,4,82,47,65,0,0,0,0,0,0,0,0,0,0},
                            {19,1,23,75,3,34,0,0,0,0,0,0,0,0,0},
                            {88,2,77,73,7,63,67,0,0,0,0,0,0,0,0},
                            {99,65,4 ,28,6,16,70,92,0,0,0,0,0,0,0},
                            {41,41,26,56,83,40,80,70,33,0,0,0,0,0,0},
                            {41,48,72,33,47,32,37,16,94,29,0,0,0,0,0},
                            {53,71,44,65,25,43,91,52,97,51,14,0,0,0,0},
                            {70,11,33,28,77,73,17,78,39,68,17,57,0,0,0},
                            {91,71,52,38,17,14,91,43,58,50,27,29,48,0,0},
                            {63,66,4,68,89,53,67,30,73,16,69,87,40,31,0},
                            {4,62,98,27,23,9,70,98,73,93,38,53,60,4,23}};

    for (short i = 0;i<15;i++) {
        for (short m=0;m<15;m++) {
            if (piramide[i][m] == 0)
                break;
            sum = countNums(i,m,piramide,15,true,true);
            if (sum > max)
                max = sum;
            sum = countNums(i,m,piramide,15,true,false);
            if (sum > max)
                max = sum;
            sum = countNums(i,m,piramide,15,false,true);
            if (sum > max)
                max = sum;
            sum = countNums(i,m,piramide,15,false,false);
            if (sum > max)
               max = sum;

        }

    }
    cout << max;
    return 0;
}


long long unsigned countNums(short start_x,short start_y,short array[][15],short size, bool goright,bool goright2) {
    long long unsigned currentSum;

    currentSum = array[start_x][start_y];

    if (goright) { //go right
        if ((start_x + 1) < size)
            start_x++;
        if ((start_y + 1) < size)
            start_y++;
    }
    else //go down
        if ((start_x + 1) < size)
            start_x++;

    if (goright2) { //still going right
        for (short i = start_x, m = start_y;i< size && m < size;i++,m++) {
            currentSum += array[i][m];         
        }
    }
    else { //still going down
        for (short i = start_x;i<size;i++) {
            currentSum += array[i][start_y];            
        }
    }

    return currentSum;
}

      

The countNums function is used to go down or diagonally. I tested this function like this:

short a = 0;
short b = 0;
cout << countNums(a,b,piramide,15,true,true) << endl;
cout << countNums(a,b,piramide,15,true,false) << endl;
cout << countNums(a,b,piramide,15,false,true) << endl;
cout << countNums(a,b,piramide,15,false,false) << endl;
return 0;

      

And it works (I also changed the function a bit so that it prints all the numbers it goes through)

But I am still not getting the correct result. It goes down and to the right and still goes to the right (adjacent numbers to the right), goes down and continues to descend (adjacent numbers to the left). What am I doing wrong here, anyone?


Alastair: It's simple

long long unsigned countNums (short start_x, short start_y, short array [] [15], short size, bool goright, bool goright2);

start_x and start_y are the coordinates of the array array is a reference to the array size is just the size of the array (it is always 15) goright needs to know if I will go down and right or just down goright2 needs to know if I will keep going down or go left ...

0


source to share


4 answers


Ok, at first, I am a bit confused about what you think about the problem. I cannot make out this second sentence at all ...

Second, you can think about your design here. Think of functions that perform a separate discrete task and are not intertwined with the rest of the application (ie read "tightly coupled and loosely coupled"). For me, the big warning bell here is the presence of an overly generic function name countNums

with a long argument list.



I think that if you split the problem into smaller, more understandable chunks, you might find problems much easier to find. I know the approach I'll take here, but I'm guessing the whole point of the exercise is to help you practice your programming skills, so I'll just leave it there ...

+3


source


I have solved this problem. Given the nature of Project Euler to solve a problem on its own ("photocopying a solved crossword does not mean that you solved it") and that I don't want to ruin it for anyone else, all I can really say is that your solution looks too much complicated.

However, you can fix this problem by reading the file. Solve it this way and problem # 69 will be a breeze!



Good luck!

+1


source


I am a little confused about this issue.
I would start by cleaning up the code.

long long unsigned countNums(short x,
                             short y,
                             short array[][15],
                             short size, 
                             bool goright,
                             bool goright2) 
{
    long long unsigned currentSum;
    currentSum = array[x][y];

    if ((x + 1) < size)    x++; //this happened in both your if cases

    if (goright && ((y + 1) < size)      y++; 

    if (goright2)
    { 
        for (;x< size && y< size;x++,y++)
            currentSum += array[x][y];         

    }
    else 
    {
        for (;x<size;x++) 
            currentSum += array[x][y];            
    }
    return currentSum;
 }

      

Now I've read the question and I'm not sure if it does what you want. Since this is not what you want, I suggest psudo-code's answer first. Forget the code. What is the answer in sudo code.
Oh, and for the love of everything that is sacred. Don't put more than one initializer in a for loop. I know I'm going to do this, but it's messy and really unnecessary. Something you might think of is a return function. Seems perfect for this problem.

0


source


The main function checks that the entry is not null, but the other function it calls does not check for that, even if it changes the index again. I don't know, I don't quite understand what you are trying to do.

I see an array size of 15 for 15 elements, will the index for the declaration also start at 0? Let me check a second. Just be sure to declare with size, but access is 0 based. Okay.

Why did you use one nested expression for statements in one place, besides that condition to be met later on? Better check what &&

has no higher priority than <

. Group 8 beats up Group 13 here . The increment operator is not used multiple times in a statement, which is good.

It looks like you've done this problem before in a different language, can you do a trace on that too and find the place where your new program differs first?

The other guys are right, the question is confusing. I would first try to solve the problem by buying subnetting in a different matrix, giving the highest score if the pyramid starts at that point and builds it from the bottom row to the top.

0


source







All Articles