Why is this not valid, but works locally? — The king's race problem.

Revision en4, by wdem, 2020-08-13 09:52:27

Could someone help a new c++ learner? — I first wrote it in python3 and got it accepted, but now i'm trying to reproduce the same results in c++ to improve my non existing coding skills and got wrong answers, which i'm unable to reproduce locally where the code works like a charm. There's seems to be some sort of misuse at line 26 where I'm comparing

if (price_coordinate_x > price_coordinate_y)

Here's my latest submission:

89767134

Here's also my code:

#include <iostream>

using namespace std;

int main(){
    // take inputs
    long int board_length;
    long int price_coordinate_x;
    long int price_coordinate_y;

    cin >> board_length;
    cin >> price_coordinate_x;
    cin >> price_coordinate_y;
    
    // starting points for kings
    int white_x = 1;
    int white_y = 1;
    long int black_x = board_length;
    long int black_y = board_length;

    // Difference between price coordinate points
    long int point_difference_white = labs(price_coordinate_x-price_coordinate_y); 
    long int point_difference_black = labs(price_coordinate_x-price_coordinate_y);
    
    // if x > y => y will be the first value we'll reach
    if (price_coordinate_x > price_coordinate_y) {
        point_difference_white += labs(white_x-price_coordinate_y);
        point_difference_black += labs(black_x-price_coordinate_x); 
    }
    else if (price_coordinate_x < price_coordinate_y) {
        point_difference_white += labs(white_x-price_coordinate_x);
        point_difference_black += labs(black_y-price_coordinate_y);
    }
    else {
        point_difference_white += labs(white_x-price_coordinate_x);
        point_difference_black += labs(black_x-price_coordinate_x);
    }
    
    if (point_difference_black >= point_difference_white) {
        cout << "White" << endl;
    }
    else if (point_difference_black < point_difference_white) {
        cout << "Black" << endl;
    }
    return 0;
}
Tags #help

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English wdem 2020-08-13 09:52:27 4
en3 English wdem 2020-08-13 09:52:04 134
en2 English wdem 2020-08-13 09:48:59 22
en1 English wdem 2020-08-13 09:47:45 1945 Initial revision (published)