-1
我试图运行一个程序,对大数的阶乘进行某些操作(比如说50!;即3.041e + 64 - 巨大!),因此不适合在我知道的正常int数据类型中(无符号long long int等)在C程序中使用适当的数据类型
我使用哪种数据类型来存储这些值?
P.S我试图在阶乘中查找尾随零。以下是我的方法:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int FactorialFinder(int a)
{
if (a>1)
a= a* (FactorialFinder(a-1)) ;
return a;
}
int main()
{
printf("Enter number \n");
int num ;
scanf("%d",&num) ;
printf("number is %d\n",num);
printf("Factorial is %d",(num = FactorialFinder(num))) ;
int x=0, count = 0 ;
while(num>0)
{
x = (num%10) ;
if (x == 0)
count++ ;
else
break;
num= num/10 ;
}
printf("\nNumber of trailing zeroes is %d",count) ;
getchar() ;
return 0;
}
工作正常12!结果是错误的(从17!它开始返回负因子值(?),从34!它给0)我猜是由于数据类型问题。有人可以帮我吗?
'n!'的小数表示形式中的尾随零数由factorial中的'5'的因子数决定。你不需要计算完整的阶乘。 – EOF
你可以用一个bignum库蛮力,但如果你从这个地方得到更多的问题,用bignum实现朴素的算法不太可能解决你所有的问题。 – user2357112
@EOF我知道这种方法,这就是为什么我没有要求解决方案。有没有使用我的方法的方法? –