Chinaunix首页 | 论坛 | 博客
  • 博客访问: 222023
  • 博文数量: 81
  • 博客积分: 1165
  • 博客等级: 少尉
  • 技术积分: 1425
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-04 11:27
文章分类

全部博文(81)

文章存档

2015年(1)

2014年(2)

2013年(37)

2012年(41)

我的朋友

分类: Java

2013-08-13 14:19:44

你在警察局工作,一天收到线报,有个炸弹被个机器人安置在某个地点,你需要迅速找到这枚炸弹。 你得到的线索是:

机器人从你所在的城市的中心出发 (0,0), 面朝北, 第一次走1000米,随后顺时针转90度,走500米。以后每次顺时针转90度,走的距离是前次的一半,那么最终它会把炸弹放在哪里?请给出坐标 (间隔小于1米就不再走了)。

用Java代码实现如下:


点击(此处)折叠或打开

  1. public class BombPoint {

  2.     public static int firstRun = 1000;
  3.     public int East = 1;
  4.     public int west = 3;
  5.     public int north = 0;
  6.     public int south = 2;

  7.     public static int getBombPoint() {
  8.         int firstRun = 1000;
  9.         int i = 0;
  10.         int[] a = new int[10];
  11.         while (firstRun >= 1) {
  12.             a[i] = firstRun;
  13.             firstRun = firstRun / 2;
  14.             
  15.             i++;
  16.         }
  17.         int j = 0;
  18.         int n = 0;
  19.         int x = 0;
  20.         int y = 0;
  21.         firstRun = 1000;
  22.         while (i >= 1) {

  23.             System.out.println(a[j]);
  24.             n = j % 4;
  25.             if (n == 0) {
  26.                 x += a[j];
  27.             } else if (n == 1) {
  28.                 y += a[j];
  29.             } else if (n == 2) {
  30.                 x -= a[j];
  31.             } else if (n == 3) {
  32.                 y -= a[j];
  33.             }
  34.             i--;
  35.             j++;
  36.         }

  37.         System.out.println("BombPoint is ("+x+"," + y+")");
  38.         System.out.println(i);
  39.         return i;
  40.     }

  41.     public static void main(String[] args) {
  42.         BombPoint.getBombPoint();
  43.     }

  44. }

阅读(753) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~