Jan 25, 2022

Euler Problem 18 - Different way (C Lang.)

Max Path Sum - Using some Another steps.

 #include <stdio.h>

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

////////////////////////////////////////////////////////////////////////////
/////////////////////////////
int max_of_two(int a, int b)
{
    if (a > b)
    {
        return a;
    }
    return b;
}

////////////////
int max_sum_triangle(int x, int y)
{
    for (x; x < 15; x++)
    {
        int zero, one;
        for (y; y < x + 1; y++)
        {
            zero = array[x - 1][y - 1] + array[x][y];
            one = array[x - 1][y] + array[x][y];
            array[x][y] = max_of_two(zero, one);
        }
    }
}

///////////////////////////
int max_fun(){
    int max_sum = 0;
    for (int i = 0; i < 15; i++)
    {
        if (array[14][i] > max_sum)
        {
            max_sum = array[14][i];
        }
       
    }
    return max_sum;
}
int main(int argc, char const *argv[])
{
    max_sum_triangle(1, 0);
    int maximum = max_fun();
    printf("%d ",maximum);

    return 0;
}

No comments:

Post a Comment

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...