02。速度控制
在约翰的汽车GPS记录每个s
秒的距离原点行驶(距离以任意但一致的单位测量)。例如,下面是一个记录的一部分s = 15
:
x = [0.0, 0.19, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25]
这些部分包括:
0.0-0.19, 0.19-0.5, 0.5-0.75, 0.75-1.0, 1.0-1.25, 1.25-1.50, 1.5-1.75, 1.75-2.0, 2.0-2.25
我们可以计算约翰的平均时速在每一个部分,我们得到:
[45.6, 74.4, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0]
鉴于s
与x
任务是返回作为一个整数*floor*
上的各部分获得每小时的最大平均速度的x
。如果x长度小于或等于1倍的回报0
,因为车子没动。
#示例:上述数据的功能,gps(x, s)
应返回74
注意
随着彩车它可能发生的结果取决于操作顺序。要计算你可以使用时速:
(3600 * delta_distance) / s
function gps(s, x) {
var arr=[];
if(x.length==0&&x.length==1){
return 0;
}
for(var i=0;i<x.length-1;i++){
var x1=x[i+1]-x[i];
console.log(x1)
arr.push(parseInt((3600*x1)/s));
}
var max=0;
for(var i=0;i<arr.length;i++){
if(max<arr[i]){
max=arr[i]
}
}
return max;
}
测试数据:
var x = [0.0, 0.23, 0.46, 0.69, 0.92, 1.15, 1.38, 1.61];
var s = 20;
var u = 41;
testing(gps(s, x), u);
x = [0.0, 0.11, 0.22, 0.33, 0.44, 0.65, 1.08, 1.26, 1.68, 1.89, 2.1, 2.31, 2.52, 3.25];
s = 12;
u = 219;
testing(gps(s, x), u);
x = [0.0, 0.18, 0.36, 0.54, 0.72, 1.05, 1.26, 1.47, 1.92, 2.16, 2.4, 2.64, 2.88, 3.12, 3.36, 3.6, 3.84];
s = 20;
u = 80;
testing(gps(s, x), u)
x = [0.0, 0.01, 0.36, 0.6, 0.84, 1.05, 1.26, 1.47, 1.68, 1.89, 2.1, 2.31, 2.52, 2.73, 2.94, 3.15];
s = 14;
u = 90;
testing(gps(s, x), u);
x = [0.0, 0.02, 0.36, 0.54, 0.72, 0.9, 1.08, 1.26, 1.44, 1.62, 1.8];
s = 17;
u = 72;
testing(gps(s, x), u);
x = [0.0, 0.24, 0.48, 0.72, 0.96, 1.2, 1.44, 1.68, 1.92, 2.16, 2.4];
s = 12;
u = 72;
testing(gps(s, x), u);
x = [0.0, 0.02, 0.44, 0.66, 0.88, 1.1, 1.32, 1.54, 1.76];
s = 17;
u = 88;
testing(gps(s, x), u);
x = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.32, 1.54, 1.76, 1.98, 2.2, 2.42, 2.76, 2.99, 3.22, 3.45];
s = 16;
u = 76;
testing(gps(s, x), u)
x = [0.0, 0.01, 0.36, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 4.0, 4.25, 4.5, 4.75];
s = 17;
u = 82;
testing(gps(s, x), u);
x = [0.0, 0.2, 0.4, 0.69, 0.92, 1.15, 1.38, 1.61, 1.92, 2.16, 2.4, 2.64, 2.88, 3.12, 3.36];
s = 19;
u = 58;
testing(gps(s, x), u);
x = [];
s = 19;
u = 0;
testing(gps(s, x), u);
x = [0.0];
s = 19;
u = 0;
testing(gps(s, x), u);
思路:数组之间的加减乘除