-4

階乗の桁数を計算した後にメモリを動的に割り当てることにより、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問正解!その後ではありません!私のソリューションの何が問題なのですか?

4

3 に答える 3

0
int main()
{
int i,j,n,m=0;

scanf("%d",&n);

int arr[2000];
int temp=0;
int inter=1;
arr[0]=1;

for(i=1;i<n;i++)
{ 
  for(j=0;j<=m;j++)
  {
    inter=arr[j]*i+temp;
    arr[j]=inter%10;
    temp=inter/10;
       if( temp>0 && j==m )
       m++;
   }
 }
for(i=m;i>=0;i++)
{
 printf("%d",arr[i]);
}
return 0;
}
于 2015-10-26T10:25:56.117 に答える