Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Python:
- 1. Read x and y from your input
- import math
- sign = 1
- movement = 1
- total_distance = 0
- while True:
- new_location = x + sign * movement
- # Case 1: FJ passes Bessie
- if (y > x and new_location >= y) or (y < x and new_location <= y):
- # FJ has passed Bessie
- total_distance = total_distance + math.abs(x-y)
- break
- # Case 2: FJ does not pass Bessie
- else:
- total_distance = total_distance + movement * 2
- sign = sign * (-1)
- movement = movement * 2
- output the total_distance
- C++/Java:
- 1. Read x and y from your input
- int sign = 1;
- int movement = 1;
- int total_distance = 0;
- while (true) {
- int new_location = x + sign * movment;
- // Case 1: FJ passes Bessie
- if (y > x && new_location >= y) || (y < x && new_location <= y) {
- // FJ has passed Bessie
- total_distance = total_distance + abs(x-y);
- break;
- }
- // Case 2: FJ does not pass Bessie
- else {
- total_distance = total_distance + movement * 2;
- }
- sign = sign * (-1);
- movement = movement * 2;
- }
- output the total_distance
- // sign = 1,-1, 1,-1, 1
- // movment = 1, 2, 4, 8, 16, ...
- x --> x+1 -> x --> x-2 -> x --> x+4 -> x --> x-8 -> x --> x+16 -> x -> y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement