我们如何用大数字计算行进距离‽
How could we calculate traveled distance, with big numbers‽
我目前正在解决这个问题:"How far I will go?"!
描述说:
You have recently discovered that horses travel in a unique pattern -
they're either running (at top speed) or resting (standing still).
Here's an example of how one particular horse might travel:
The horse Blaze can run at 14 metres/second for 60 seconds, but must
then rest for 45 seconds.
After 500 seconds Blaze will have traveled 4200 metres.
Your job is to write a function that returns how long a horse will
have traveled after a given time.
输入:
totalTime - How long the horse will be traveling (in seconds)
runTime - How long the horse can run for before having to rest (in seconds)
restTime - How long the horse have to rest for after running (in seconds)
speed - The max speed of the horse (in metres/second)
我已经手算过微积分了:
给定:速度 = 14,运行时间 = 60,休息时间 = 45,总时间 = 500
-> 要计算马 运行 需要多少时间,我们计算如下:
totalTime / (运行Time + restTime) = 500 / (60 + 45) = 500 / 105 = 4.76...
roundUp(总时间)= 5
总运行时间 = 5 * 运行时间 = 5 * 60 = 300
--> 计算马走了多少距离:
x = 速度 * 总运行时间 = 14 * 300 = 4200
我翻译成如下代码:
public class Kata {
public static int travel(int totalTime, int runTime, int restTime, int speed) {
int totalRunTime = 0;
if(totalTime < runTime){
totalRunTime = totalTime;
}else{
totalRunTime = (int)Math.ceil( (double)totalTime/(runTime + restTime) ) * runTime;
}
return speed * totalRunTime;
}
}
当我们运行进行基本测试时,它给出了预期的输出:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class KataTest {
@Test
public void exampleTests() {
assertEquals(4200, Kata.travel(500, 60, 45, 14));
assertEquals(1120, Kata.travel(1000, 10, 127, 14));
assertEquals(1000, Kata.travel(100, 10, 0, 10));
assertEquals(1000, Kata.travel(100, 10, 0, 10));
assertEquals(450, Kata.travel(25, 50, 120, 18));
}
}
然而,当我们用高级示例测试它时,使用更大的数字,它显示不准确的结果。
例如:
给定:速度 = 6,运行时间 = 34,休息时间 = 180,总时间 = 99094;预期的输出是 94524,但是我发布的代码给了我们 94656
我手写了跟踪以了解我的代码的作用:
->计算本马总时间运行s:
totalTime / (运行Time + restTime) = 99094 / (34 + 180) = 99094 / 214 = 463.05...
roundUp(总时间)= 464
总运行时间 = 464 * 运行时间 = 464 * 34 = 15776
--> 计算马走了多远:
x = 速度 * 总运行时间 = 6 * 15776 = 94656
那么失败的测试是:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class KataTest {
@Test
public void biggerTests() {
assertEquals(84954920, Kata.travel(35869784, 90, 100, 5));
}
}
我的直觉是double到int的转换或者Math.ceil的舍入都需要改进代码,但不知道为什么,也不知道如何解决。这是我认为我们可以改进的行:
totalRunTime = (int)Math.ceil( (double)totalTime/(runTime + restTime) ) * runTime;
我也看过:
- Calculate traveled distance
- AnyLogic: How to calculate pedestrian traveled distance?
- Calculate distance traveled (not distance between)
- https://www.tutorialspoint.com/java/lang/math_ceil.htm
问题不在于四舍五入。
我们来看例子 travel(35869784, 90, 100, 5)
.
根据您的公式 35869784/(100+90) = 188788.336
,四舍五入得到 188789,乘以 runTime
,乘以 speed
得到 84,955,050 米,而正确答案是 84,954,920。这不是一个舍入问题。公式错了
为什么?考虑最后休息后的运行。这匹马已经 运行 188,788 次 90 秒 + 100 秒的完整迭代,在此期间它在 35,869,720 秒内跑了 84,954,600 米的距离。在 "full" 运行 秒之后,马现在只剩下 64 秒到 运行,这比 runTime
!
还少
这匹马 运行 在 64 秒内跑多远? 320 米。所以总共是 84,954,600 + 320 = 84,954,920
米。
我目前正在解决这个问题:"How far I will go?"!
描述说:
输入:You have recently discovered that horses travel in a unique pattern - they're either running (at top speed) or resting (standing still).
Here's an example of how one particular horse might travel:
The horse Blaze can run at 14 metres/second for 60 seconds, but must then rest for 45 seconds.
After 500 seconds Blaze will have traveled 4200 metres.
Your job is to write a function that returns how long a horse will have traveled after a given time.
totalTime - How long the horse will be traveling (in seconds)
runTime - How long the horse can run for before having to rest (in seconds)
restTime - How long the horse have to rest for after running (in seconds)
speed - The max speed of the horse (in metres/second)
我已经手算过微积分了:
给定:速度 = 14,运行时间 = 60,休息时间 = 45,总时间 = 500
-> 要计算马 运行 需要多少时间,我们计算如下:
totalTime / (运行Time + restTime) = 500 / (60 + 45) = 500 / 105 = 4.76... roundUp(总时间)= 5
总运行时间 = 5 * 运行时间 = 5 * 60 = 300
--> 计算马走了多少距离:
x = 速度 * 总运行时间 = 14 * 300 = 4200
我翻译成如下代码:
public class Kata {
public static int travel(int totalTime, int runTime, int restTime, int speed) {
int totalRunTime = 0;
if(totalTime < runTime){
totalRunTime = totalTime;
}else{
totalRunTime = (int)Math.ceil( (double)totalTime/(runTime + restTime) ) * runTime;
}
return speed * totalRunTime;
}
}
当我们运行进行基本测试时,它给出了预期的输出:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class KataTest {
@Test
public void exampleTests() {
assertEquals(4200, Kata.travel(500, 60, 45, 14));
assertEquals(1120, Kata.travel(1000, 10, 127, 14));
assertEquals(1000, Kata.travel(100, 10, 0, 10));
assertEquals(1000, Kata.travel(100, 10, 0, 10));
assertEquals(450, Kata.travel(25, 50, 120, 18));
}
}
然而,当我们用高级示例测试它时,使用更大的数字,它显示不准确的结果。
例如:
给定:速度 = 6,运行时间 = 34,休息时间 = 180,总时间 = 99094;预期的输出是 94524,但是我发布的代码给了我们 94656
我手写了跟踪以了解我的代码的作用:
->计算本马总时间运行s:
totalTime / (运行Time + restTime) = 99094 / (34 + 180) = 99094 / 214 = 463.05... roundUp(总时间)= 464
总运行时间 = 464 * 运行时间 = 464 * 34 = 15776
--> 计算马走了多远:
x = 速度 * 总运行时间 = 6 * 15776 = 94656
那么失败的测试是:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class KataTest {
@Test
public void biggerTests() {
assertEquals(84954920, Kata.travel(35869784, 90, 100, 5));
}
}
我的直觉是double到int的转换或者Math.ceil的舍入都需要改进代码,但不知道为什么,也不知道如何解决。这是我认为我们可以改进的行:
totalRunTime = (int)Math.ceil( (double)totalTime/(runTime + restTime) ) * runTime;
我也看过:
- Calculate traveled distance
- AnyLogic: How to calculate pedestrian traveled distance?
- Calculate distance traveled (not distance between)
- https://www.tutorialspoint.com/java/lang/math_ceil.htm
问题不在于四舍五入。
我们来看例子 travel(35869784, 90, 100, 5)
.
根据您的公式 35869784/(100+90) = 188788.336
,四舍五入得到 188789,乘以 runTime
,乘以 speed
得到 84,955,050 米,而正确答案是 84,954,920。这不是一个舍入问题。公式错了
为什么?考虑最后休息后的运行。这匹马已经 运行 188,788 次 90 秒 + 100 秒的完整迭代,在此期间它在 35,869,720 秒内跑了 84,954,600 米的距离。在 "full" 运行 秒之后,马现在只剩下 64 秒到 运行,这比 runTime
!
这匹马 运行 在 64 秒内跑多远? 320 米。所以总共是 84,954,600 + 320 = 84,954,920
米。