-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheLostCow.java
More file actions
35 lines (32 loc) · 928 Bytes
/
Copy pathTheLostCow.java
File metadata and controls
35 lines (32 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.*;
import java.util.*;
public class TheLostCow {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(new File("lostcow.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("lostcow.out")));
int x = scan.nextInt();
int y = scan.nextInt();
int toMove = 1;
int dir = 1;
int dist = 0;
int prev = x;
int loc = x;
while(x != y) {
loc = x + toMove * dir;
dist += Math.abs(prev - loc);
toMove *= 2;
if(loc > y && x < y) {
dist -= (loc -y);
break;
}
else if(loc < y && x > y) {
dist -= (y-loc);
break;
}
dir *= -1;
prev = loc;
}
pw.println(dist);
pw.close();
}
}