我正在尝试编写一个代码,通过以下公式计算带有尾递归的 intiger 的双阶乘:DoubleFactorial。
这是我的代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// Test cases: 5!! = 15; 10!! = 3840;
long long Factorial(int n) {
if (n < 0) // assignment assumption
return 0;
if (n == 0) // stopping condition
return 1;
return n * Factorial(n - 1); // non-tail recursive step
}
int tail_recursion_double_factorial(int n, int accumulator) {
if (n < 0) // assignment assumption
return 0;
if (n == 0) // stopping condition
return accumulator;
if (n == 1) // stopping condition -- return the most updated counter
return accumulator;
return Factorial(n) / tail_recursion_double_factorial(n - 1, n*accumulator); // TAIL RECURSIVE step -- using formula for double factorial
}
int main() {
int n;
long long res; //res doesn't have to be of type int.
printf("Please enter a number:\n");
scanf("%d", &n);
res = tail_recursion_double_factorial(n,1); // assigning the recursive call to a variable
printf("%d!! = %ld", n, res); // NOTE: IF YOU CHANGE THE TYPE OF RES - CHANGE THE SPECIAL CHARACTHER SPECIFIER (%) ACCORDINGLY
return 0;
}
由于某种原因,我收到以下错误: 错误
我尝试了一切,但我不确定是什么导致了这个错误——我看不出除零问题可能发生在哪里。