では、サルが x の高さのポールを登るのにかかる時間を計算するという課題があります。休憩中に 10 分間で 10 フィート上昇し、10 分間で 3 フィート滑り落ちる場合。ポールの頂上に到達すると停止し、1 分間に 1 フィート上昇します。これは再帰を使用しており、私の方法はこれまでのところ正しいですが、スタックオーバーフローが発生していて、それを回避する方法がわからないので、気が狂っていますか?
みんなありがとう
public static long climb(long height, long time) { //Recursive method
if (height <= 0) //base case
return time;
/*Every 10 min add 3 to height (notice im doing it backwards,
instead of substracting 3.
I find it easier to deal with this way and easier to implement the base case) *//
else if (time != 0 && time % 10 == 0) {
return climb(height+3, time+10);
}
else {
return climb(height-1, time+1);
} } }