로깅을 시도하십시오. 또는, 음, 단지 디버그 - 인쇄 :
public static int h(int n){
System.out.println("called h(" + n + ")");
if (n == 0) {
System.out.println("we know the result for 0, returning 0");
return 0;
} else {
System.out.println("we don't know the result, calling for " + (n-1));
int t = h(n-1);
System.out.println("Found the result for " + (n-1) +
", calculating the result for " + n);
return t + 1;
}
}
n = 4
를 들어, 당신은 얻을 것이다 :
called h(4)
we don't know the result, calling for 3
called h(3)
we don't know the result, calling for 2
called h(2)
we don't know the result, calling for 1
called h(1)
we don't know the result, calling for 0
called h(0)
we know the result for 0, returning 0
Found the result for 0, calculating the result for 1
Found the result for 1, calculating the result for 2
Found the result for 2, calculating the result for 3
Found the result for 3, calculating the result for 4
은 당신에게 단서를주지 희망 - 어떻게되는지, 다른 알고리즘을 재생할 수 있습니다.
또한 h(-1)
을 호출하여 재미있게 사용해보십시오. 당신은 재귀의 개념을 이해하는데 어려움이있는 경우 모든
출처
2011-12-17 14:34:35
alf
당신이 '추적'은 무엇을 의미합니까
희망? System.out.println ("h ("+ n + ")"); 전에 if (n == 0), 당신은 h (5)에 대한 출력으로 무엇을 기대합니까? – milan
당신이 말하는 것처럼 간단한 재귀 적 방법입니다. 여기서 당신 혼란의 근원은 무엇입니까? 특별히 언급하십시오. – Lion
정확히 추적하려고하는 대상은 무엇입니까? 이것은 재귀 함수의 3 가지 조건을 모두 만족시킵니다. – tarheel