Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
誰かが私にこれを説明してもらえますか。
public int bunnyEars(int bunnies) { if(bunnies == 0) { return 0; } return 2 + bunnyEars(bunnies - 1); }
うさぎが 2 に等しい場合、答えは 4 ではなく 3 になるように思えます。答えが 4 であることはわかっていますが。
手動で再帰をアンロールして、すべてのバニーに 2 つの耳が実際に追加されることを確認できます。
bunnyEars(2) => 2 + bunnyEars(1) => 2 + (2 + bunnyEars(0)) => 2 + (2 + 0) => 4
bunnyEars(2) == 2 + bunnyEars(1) == 2 + (2 + bunnyEars(0)) == 2 + (2 + 0) == 4