HDU 1042求N! (大数)

阶乘递归代码:(教材)
C++代码   收藏代码
  1. #include<stdio.h>  
  2. int f(int n);  
  3. void main()  
  4. {  
  5.   printf("%d\n",f(5));  
  6. }  
  7. int f(int n)  
  8. {  
  9.   if(n==0)return 1;  
  10.   return n*f(n-1);  
  11. }  



============================================================
大数阶乘问题
10000以内的阶乘及位数:
============================================================
代码一:
C++代码   收藏代码
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int carry,n,j;  
  5.     int a[40001];  
  6.     int digit;  
  7.     int temp,i;     
  8.     while(scanf("%d",&n)!=EOF){  
  9.     a[0]=1;digit=1;  
  10.     for(i=2; i<=n; i++)  
  11.     {  
  12.         for(carry=0,j=1; j<=digit; ++j)  
  13.         {  
  14.             temp=a[j-1]*i+carry;  
  15.             a[j-1]=temp%10;  
  16.             carry=temp/10;  
  17.         }  
  18.         while(carry)  
  19.         {  
  20.             //digit++;  
  21.             a[++digit-1]=carry%10;  
  22.             carry/=10;  
  23.         }  
  24.     }  
  25.      
  26.     for(int k=digit; k>=1; --k)  
  27.         printf("%d",a[k-1]);  
  28.     printf("\n");  
  29.  printf("length=%d\n",digit);  
  30.  }  
  31.     return 0;  
  32. }  



代码二:
C++代码   收藏代码
  1. #include <iostream>    
  2. using namespace std;   
  3. void main()    
  4. {    
  5.   int result[40000]; //保存结算结果的数组     
  6.   int height = 1;  //结果的最高位     
  7.   int num;  //计算阶乘的数字     
  8.   cin>>num;     
  9.   result[0] = 1;    
  10.   for (int i=1;i<=num;i++)    
  11.   {    
  12.     int res = 0; //进位     
  13.     for (int j=0;j<height;j++)    
  14.     {    
  15.       int buf = result[j] * i + res; //计算结果     
  16.       result[j] = buf % 10;  //取当前位     
  17.       res = buf / 10;   //计算进位     
  18.     }    
  19.     while (res)    
  20.     {    
  21.       result[height++] = res % 10; //取当前位     
  22.       res /= 10;   //计算进位     
  23.     }      
  24.   }      
  25.     
  26.   for (int k=height-1;k>=0;k--)    
  27.   {    
  28.      cout<<result[k];    
  29.   }    
  30.   cout<<endl;    
  31.   cout<<"length="<<height<<endl;    
  32. }   


============================================================
例如1000阶乘位数:
log10(1)+log10(2)+•••+log10(1000)取整后加1
============================================================
C++代码   收藏代码
  1. #include<stdio.h>  
  2. #include<math.h>  
  3. void main()  
  4. {  
  5.   int n,i;  
  6.   double d;  
  7.   scanf("%d",&n);  
  8.   d=0;  
  9.   for(i=1;i<=n;i++)  
  10.       d+=log10(i);  
  11.   printf("%d\n",(int)d+1);  
  12. }  



============================================================
高效算法
斯特林公式(stirling)求阶乘的位数:
============================================================
C++代码   收藏代码
  1. #include<stdio.h>  
  2. #include<math.h>  
  3. #define PI 3.1415926  
  4. int main()  
  5. {  
  6. int cases,n,ans;  
  7. scanf("%d",&cases);  
  8. while(cases--)  
  9. {  
  10.    scanf("%d",&n);  
  11.    if(n==1)  
  12.     printf("1\n");  
  13.    else  
  14.    {  
  15.      ans=ceil((n*log(n)-n+log(2*n*PI)/2)/log(10));  
  16.       printf("%d\n",ans);  
  17.    }  
  18. }  
  19. return 0;  
  20. }  


============================================================
阶乘最后非零位:
============================================================
C++代码   收藏代码
  1. #include<stdio.h>   
  2. #include<string.h>   
  3. #define maxn 10000   
  4. int lastdigit(char buf[])   
  5. {   
  6.     const int mod[20]={1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2};   
  7.     int len=strlen(buf),a[maxn],i,c,ret=1;   
  8.     if(len==1)   
  9.         return mod[buf[0]-'0'];   
  10.     for(i=0;i<len;i++)   
  11.         a[i]=buf[len-1-i]-'0';   
  12.     for(;len;len-=!a[len-1])   
  13.     {   
  14.         ret=ret*mod[a[1]%2*10+a[0]]%5;   
  15.         for(c=0,i=len-1;i>=0;i--)   
  16.             c=c*10+a[i],a[i]=c/5,c%=5;   
  17.     }   
  18.     return ret+ret%2*5;   
  19. }   
  20. int main()   
  21. {   
  22.     char n[maxn];   
  23.     int a;   
  24.     while(scanf("%s",n)!=EOF)   
  25.     {   
  26.         a=lastdigit(n);   
  27.         printf("%d\n",a);   
  28.     }   
  29.     return 0;   


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值