对于该递归Java程序,我无法在我的课程中匹配相同的输出。对于整数76
和84
,递归调用的数量应为3,但我得到的是4。我的教授的样本输出是否错误?
package Week13;
import java.util.Scanner;
public class Assignment12_Part1 {
static int counter;
public static void main(String[] args) {
counter = 0;
System.out.print("Please enter two integers space seperated: ");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
System.out.println("The GCD of " + a + " and " + b + " is " + gcd(a, b));
System.out.printf("Total %4d recursive calls", counter);
input.close();
}
public static int gcd(int a, int b) {
counter++;
if (b == 0) return a;
else if (a == 0) return b;
else if (a % b == 0) return b;
else return gcd(b, a % b);
}
}