階乗の桁数を計算した後にメモリを動的に割り当てることにより、100 のような非常に大きな数の階乗を c で計算しようとしています。私のコードは次のとおりです。
int main()
{
int n,q,i,j,z,t,d;
float p=0.0;
printf("Enter the number whose factorial is to be calculated:\n");
scanf("%d",&n);
//calculating number of digits
for(j=2;j<=n;j++)
p=p+log10(j);
d=(int)p+1;
printf("No of digits in the factorial are:%d\n",d);
int *a;
a=(int *)malloc(d*sizeof(int));//allocation of memory
a[0]=1;
for(i=1;i<n;i++)//initialize array
a[i]=0;
p=0.0;
for(j=2;j<=n;j++)
{
q=0;
p=p+log10(j);
z=(int)p+1;
for(i=0;i<z;i++)
{
t=(a[i]*j)+q;
q=t/10;
a[i]=t%10;
}
}
printf("\n");
for(i=d-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
最大40問正解!その後ではありません!私のソリューションの何が問題なのですか?