zoukankan      html  css  js  c++  java
  • 自己写的复杂点的大数阶乘(会超时,有助于理解大数相乘)

    #include<stdio.h>
    #include<string.h>
    #define MAX(x,y) x>y?x:y
    char z[100010];
    int j;
    void bigmultiply(int n){int x[100010],y[100010];
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
        int t=j,i,temp=0,max=0;
        for(i=t-1,j=0;i>=0;--i,j++)x[j]=z[i]-'0';
        for(i=0;i<t;++i){j=i;
            temp=x[i]*n;
            while(temp){
                y[j]+=temp%10;
                if(y[j]>9)y[j+1]+=y[j]/10,y[j]%=10;
                 j++;
                temp/=10;
            }
            while(y[j])j++;
            max=MAX(max,j);
        }
        for(i=max-1,j=0;i>=0;--i,j++)z[j]=y[i]+'0';
        z[j]='';
    }
    int main(){
        int m,t;
        while(~scanf("%d",&m)){memset(z,0,sizeof(z));z[0]='1';j=1;
        //bigmultiply(m);
            for(int i=1;i<=m;++i)bigmultiply(i);
            printf("%s
    ",z);
        }
        return 0;
    }

    借助模板写的:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define MAXN 100010
     4 #define MAX(x,y) x>y?x:y
     5 int m[MAXN];
     6 void fac(int n){int i,j,temp1,temp2,t=0;
     7 m[0]=1;
     8     for(i=2;i<=n;++i){temp2=0;
     9         for(j=0;j<=t||temp1;++j){
    10             temp1=m[j]*i+temp2;
    11             m[j]=temp1%10;
    12             temp2=temp1/10;
    13             t=MAX(t,j);
    14         }
    15     }
    16     for(j=MAXN-1;j>=0;j--)if(m[j])break;
    17     for(i=j;i>=0;i--)printf("%d",m[i]);
    18     puts("");
    19 }
    20 int main(){
    21     int N;
    22     while(~scanf("%d",&N)){memset(m,0,sizeof(m));
    23      fac(N);
    24     }
    25     return 0;
    26 }
  • 相关阅读:
    final有什么用?
    数组的定义
    作业
    List 、Set数据结构
    报表工具实现单据套打
    动态格报表的制作
    图形钻取
    报表工具轻松搞定卡片式报表
    列表钻取
    报表中如何实现不规则布局
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4674180.html
Copyright © 2011-2022 走看看