D
D
Dmitry Shevchenko2021-12-20 17:23:43
C++ / C#
Dmitry Shevchenko, 2021-12-20 17:23:43

Inappropriately high value?

The algorithm must find the minimum values ​​from a two-dimensional vector and add them to the cost variable until the condition is met, but the answer is -15166, although there can be no negative numbers there. I don't know where to dig already..

short cost = 0 ,mass=0;


    short min=10000, min_x, min_y;
    for (; true;) { //поиск минимальных значений 
        for (int i = 0; i < x; i++) {
            for (int k = 0; k < y; k++) {
                if (field_price[i][k] < min && field_price[i][k] != 0) {
                        min = field_price[i][k];
                        min_x = i;
                        min_y = k;
                }
            }
        }
        mass += field_mass[min_x][min_y];
        field_price[min_x][min_y] = 10000;
        if (mass > size) {
            cout << cost;
            return 1;
        }
        cost += min;
        min = 100000;
        
    }

I write a large value in the cells so that they are not defined next time as the minimum, and there cannot be more than 1000 values ​​either.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2021-12-20
@ZER0x32

You have an overflow. See
Type Limits

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question