beginAddress是开始抓拍路口编码,endAddress是结束抓拍路口编码,
shootCars是所有抓拍路口抓拍车辆信息(每个车辆在每个路口仅被抓拍1次) ;求路段平均旅行时间 (单位:分钟),结果保留两位小数。
当时笔试,没有做出来,后面想了一下,这么写了,网上也没有固定的答案,不知道对不对,还请各位大佬给看看!听说大华实习也是出的这道题
public class shootCar { private int adrId; private String carNum; private Date shootTime; } public class ShootCarService{ public float getAvgTravel(int beginAddress, int endAddress, List<shootCar> shootCars) { HashMap<String, Long> map = new HashMap<>(); // 所有经过[开始抓拍,结束抓拍]的车辆时间总和 long travelTime = 0; // 经过抓拍路段的车辆数目 int carNums = 0; for (shootCar car : shootCars) { // Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT long time = car.shootTime.getTime(); // 毫秒-》秒=1000,再除60是分钟 System.out.println(car.carNum + "," + car.shootTime + "," + time); // 车辆进入抓拍路段,放入map if (car.adrId == beginAddress) { map.put(car.carNum, time); } // 车辆驶离抓拍路段 else if (car.adrId == endAddress) { // 车辆走完了抓完路段,即进入开始抓拍、驶离结束抓拍 if (map.containsKey(car.carNum)) { travelTime = (time - map.get(car.carNum)) / 60000; carNums += 1; } } } return (int) (travelTime / carNums * 100)/ 100; } }
全部评论
(4) 回帖