zoukankan      html  css  js  c++  java
  • 高精度阶乘

    输入n,计算n的阶乘

    例如:30!= 265252859812191058636308480000000

    思路:利用数组保存计算结果

    245*5    ==> 

    5*5=25          5

    4*5+2=22    2

    2*5+2=12    2

    0*5+1=1    1

    结果为5221

        

    //阶乘的精度值 
    #include<iostream>
    #include<cstring> 
    #define MAX 3000
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        int s[MAX + 1];     
        memset(s, 0, sizeof(s));     //将数组中的所有元素设为0,这步不能省、不能省、不能省 
        s[0] = 1;                   //初始值设为1 
        
        for(int i = 2; i <= n; i++)
        {
            int c = 0;
            for(int j = 0; j < MAX; j++)
            {        
                int t = s[j] * i + c;
                s[j] = t % 10;
                c = t / 10;    
                /*if(s[j+1] == 0 && c == 0)    //想想乘积中间很可能会有零,不能加这句。应该有更好的降低时间复杂度的方法 
                    break;*/    
            }        
        }
        
        int m = MAX;
        while(!s[m] && m > 0) m--;
        for(; m >= 0; m--)
            cout << s[m];
        cout << endl;
        return 0;
    }
  • 相关阅读:
    七月份文章收藏
    五月份文章收藏
    二月份文章收藏
    一月份文章收藏
    nmap数据流
    memcached未授权
    Redis未授权漏洞利用方式
    requests sslerror
    欢迎来到Curl的世界
    Dig命令解析结果
  • 原文地址:https://www.cnblogs.com/fengyanlover/p/5292588.html
Copyright © 2011-2022 走看看