wdem's blog

By wdem, history, 4 years ago, In English

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;
}

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it