惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
V
Vulnerabilities – Threatpost
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
Y
Y Combinator Blog
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
MyScale Blog
MyScale Blog
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
量子位
I
Intezer
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
L
Lohrmann on Cybersecurity
L
LINUX DO - 最新话题
The Register - Security
The Register - Security
T
Tailwind CSS Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
Stack Overflow Blog
Stack Overflow Blog
Cloudbric
Cloudbric
S
Secure Thoughts
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
L
LangChain Blog
Recorded Future
Recorded Future
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
人人都是产品经理
人人都是产品经理
F
Full Disclosure
O
OpenAI News
Webroot Blog
Webroot Blog
A
Arctic Wolf
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
H
Heimdal Security Blog
B
Blog RSS Feed
Vercel News
Vercel News

博客园 - Love Fendi

性能优化系列---查询高cup的sql State模式学习 8.17--8.24积累 sql server 2005 analysis service step by step(三):创建父子维度 sql server 2005 analysis service step by step(二):创建时间维度 sql serve 2005 analysis service step by step(一):创建标准维度 算法练习五:求数组中第k大的数 算法练习三:奇偶分割 算法练习二:二分查找 数据库锁 算法练习一:最大公约数与最小公倍数 索引优化 生成验证码,同时异步获取加密后的验证码 自定义控件中与脚本资源集成的若干处理方式 一条语句删除表中某字段重复的数据 动态按需异步加载js文件 在Nhibernate中使用Json.net中出现Self referencing loop的错误的处理 JQuery学习笔记 c#委托事件 入门
算法练习四:求N!不溢出
Love Fendi · 2009-04-07 · via 博客园 - Love Fendi

  思想:普通的按递归的方法求阶层的话,到70的时候就会溢出了。所以采用数组去保存结果的每一位数字,

static void Main(string[] args)
        {
            int[] a = CalculateLargeNumber(5);
            for (int i = a.Length-1; i >= 0; i--)
                Console.Write(a[i]);

            Console.Read();


        }
        public static int[] CalculateLargeNumber(int n)
        {

            if (n < 0) { throw new ArgumentOutOfRangeException("n必须为非负数。"); }

            if (n == 0 || n == 1) { return new int[] { 1 }; }
            // 数组的最大长度
            const int MaxLength = 100000;
            int[] array = new int[MaxLength];
            // 1! = 1
            array[0] = 1;
            int i = 0;
            int j = 0;
            // valid为当前阶乘值的位数(如5! = 120,此时valid = 3)
            int valid = 1;
            for (i = 2; i <= n; i++)
            {
                long carry = 0;
                for (j = 0; j < valid; j++)
                {
                    long multipleResult = array[j] * i + carry;
                    // 计算当前位的数值
                    array[j] = (int)(multipleResult % 10);
                    // 计算进到高位的数值
                    carry = multipleResult / 10;

                }
                // 为更高位赋值
                while (carry != 0)
                {
                    array[valid++] = (int)(carry % 10);
                    carry /= 10;

                }

            }
            int[] result = new int[valid];
            Array.Copy(array, result, valid);
            return result;
        }
    }