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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threatpost
Project Zero
Project Zero
Know Your Adversary
Know Your Adversary
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
T
Tenable Blog
Scott Helme
Scott Helme
T
Tor Project blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
NISL@THU
NISL@THU
Cisco Talos Blog
Cisco Talos Blog
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
S
Securelist
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
Cloudbric
Cloudbric
C
Check Point Blog
Jina AI
Jina AI
Webroot Blog
Webroot Blog
量子位
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
罗磊的独立博客
H
Heimdal Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
人人都是产品经理
人人都是产品经理
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LINUX DO - 热门话题
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
PCI Perspectives
PCI Perspectives
Vercel News
Vercel News
N
News | PayPal Newsroom
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美
博客园 - 叶小钗
Y
Y Combinator Blog
V
V2EX
美团技术团队
O
OpenAI News
Microsoft Security Blog
Microsoft Security Blog
AWS News Blog
AWS News Blog

博客园 - 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;
        }
    }