5

アルゴリズムを変更して、再帰呼び出しの量をさらに表示するにはどうすればよいですか?

public class fibb {

    static long fibonacci(long n){
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return fibonacci(n - 1) + fibonacci(n - 2);
    }

    public static void main(String[] args){
        System.out.println(fibonacci(14));
    }
}
4

4 に答える 4

4

静的変数を使用して、再帰呼び出しの数を保持できます。

public class fibb {
    public static int count;
    static long fibonacci(long n){
        count++;
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return fibonacci(n - 1) + fibonacci(n - 2);
    }

    public static void main(String[] args){
        System.out.println(fibonacci(14));
        System.out.println("Number of times fibonacci function called is :" +count);
    }
}
于 2013-05-25T08:53:20.110 に答える
2

カウンターと結果の両方に別のクラスをお勧めします。

class Counter {
    private int value = 0;

    public void inc() {
        value++;
    }

    public int get() {
        return value;
    }
}

class Result {
    public final long value;
    public final int calls;

    public Result(long value, int calls) {
        super();
        this.value = value;
        this.calls = calls;
    }

    public final long getValue() {
        return this.value;
    }

    public final int getCalls() {
        return this.calls;
    }
}

このように使われる

public class FibonacciWithCounter {
    static Result fibonacci(long n) {
        final Counter counter = new Counter();
        final long value = fibonacci(n, counter);
        return new Result(value, counter.get());
    }

    static long fibonacci(long n, Counter counter) {
        counter.inc();

        if (n == 0)
            return n;
        else if (n == 1)
            return n;
        else 
            return fibonacci(n - 1, counter) + fibonacci(n - 2, counter);

    }

    public static void main(String[] args) {
        final Result result = fibonacci(14);
        System.out.println("Result is " + result.value 
              + " (" + result.getCalls() + " calls)");
    }
}

したがって、静的なものはなく、各パラメーター/クラスはその目的を明確に説明しています。このバージョンが最も長いバージョンであっても (追加のクラスのボイラー プレートのため)、より表現力があるため、私はこれを好みます。

于 2013-05-25T22:14:46.813 に答える
2

静的カウンターを使用したくない場合は、カウンターのパラメーターを追加できます。長さ 1 の配列など、呼び出し元に表示される方法で変更できるように、カウンターをオブジェクトにラップする必要があります。

public class fibb {

    static long fibonacci(long n) {
        return fibonacci(n, null);
    }

    static long fibonacci(long n, long[] counter){
        if (counter != null && counter.length > 0) ++counter[0];
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return fibonacci(n - 1, counter) + fibonacci(n - 2, counter);
    }

    public static void main(String[] args){
        long n = args.length > 0 ? Long.parseLong(args[0]) : 14;
        long[] counter = new long[1];
        System.out.println(fibonacci(n, counter));
        System.out.println(counter[0] + " calls to fibonacci.");
    }
}
于 2013-05-25T08:54:03.163 に答える
0

静的変数は一度だけ初期化されるため、静的カウンター変数を作成し、再帰の最初の行で 1 ずつインクリメントできます。

public class fibb {

    private static int numberOfCalls = 0;

    static long fibonacci(long n){
        numberOfCalls++;
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return fibonacci(n - 1) + fibonacci(n - 2);
    }

    public static void main(String[] args){
        System.out.println(fibonacci(14));
        //numberOfCalls is the number of the calls to the recursion
    }
}
于 2013-05-25T08:49:04.580 に答える