你在警察局工作,一天收到线报,有个炸弹被个机器人安置在某个地点,你需要迅速找到这枚炸弹。 你得到的线索是:
机器人从你所在的城市的中心出发 (0,0), 面朝北, 第一次走1000米,随后顺时针转90度,走500米。以后每次顺时针转90度,走的距离是前次的一半,那么最终它会把炸弹放在哪里?请给出坐标 (间隔小于1米就不再走了)。
用Java代码实现如下:
-
public class BombPoint {
-
-
public static int firstRun = 1000;
-
public int East = 1;
-
public int west = 3;
-
public int north = 0;
-
public int south = 2;
-
-
public static int getBombPoint() {
-
int firstRun = 1000;
-
int i = 0;
-
int[] a = new int[10];
-
while (firstRun >= 1) {
-
a[i] = firstRun;
-
firstRun = firstRun / 2;
-
-
i++;
-
}
-
int j = 0;
-
int n = 0;
-
int x = 0;
-
int y = 0;
-
firstRun = 1000;
-
while (i >= 1) {
-
-
System.out.println(a[j]);
-
n = j % 4;
-
if (n == 0) {
-
x += a[j];
-
} else if (n == 1) {
-
y += a[j];
-
} else if (n == 2) {
-
x -= a[j];
-
} else if (n == 3) {
-
y -= a[j];
-
}
-
i--;
-
j++;
-
}
-
-
System.out.println("BombPoint is ("+x+"," + y+")");
-
System.out.println(i);
-
return i;
-
}
-
-
public static void main(String[] args) {
-
BombPoint.getBombPoint();
-
}
-
-
}
阅读(777) | 评论(1) | 转发(0) |