加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

1053 -- 大数阶乘

发布时间:2020-12-14 02:22:49 所属栏目:大数据 来源:网络整理
导读:大数阶乘 Time Limit:3000MS? Memory Limit:65535K Total Submit:245 Accepted:99 Description 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它? Input 输入一个整数m( 0 m =5000) Output 输出m的阶乘,并在输出结束

大数阶乘

Time Limit:3000MS? Memory Limit:65535K
Total Submit:245 Accepted:99

Description

我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?

Input

输入一个整数m( 0 < m < =5000)

Output

输出m的阶乘,并在输出结束之后输入一个换行符

Sample Input

50

Sample Output

30414093201713378043612608166064768844377641568960512000000000000

Hint

C 用数组
Java 用BigInteger

Source

NYIST

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace AK1053 {
        class Program {
            static void Main(string[] args) {
                string sb;
                int i,j,temp,start,sc,N = 1200;
                while ((sb = Console.ReadLine()) != null) {
                    int n = int.Parse(sb);
                    int[] a = new int[N];
                    for (a[0] = 1,i = 1; i <= n; i++)
                        for (sc = 0,j = 0; j < N; j++) {
                            temp = a[j] * i + sc;//当前这一位的数乘上i,然后加上后面来的进位
                            sc = temp / 10;//sc代表进位
                            a[j] = temp % 10;//本位保留模后的结果
                        }
                    //数据是倒着存的,即1*2*3*4=24,存是是按420000000...保存的,一直有N=1200位
                    for (start = N - 1; a[start] == 0; --start) ;//这里是确定从后面哪一位开始输出,从后向前输出
                    for (; start >= 0; --start)
                        Console.Write(a[start]);
                    Console.WriteLine();
                }
            }
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读