• c# 求一定的大阶乘


    1.通过List<int> 类型存储

    代码:

       private List<int> fact(int n)
           {
               int s = 1;
               int m = 0;
                int a,b,c;
               List<int> lstfact = new List<int>();
               List<int> lstresult = new List<int>();
               lstfact.Add(1);
               for (int i = 1; i <= n; i++)
               {
                   m = 0;
                for (int k = 0; k < lstfact.Count;k++ )
                {
                    s = i * lstfact[k];
                    s = s + m;
                    a = s / 10;
                    b = s % 10;
                    lstfact[k] = b;
                    if (a>0)
                    {
                        if (k<lstfact.Count-1)
                        {
                            m = a;
                        }
                        else
                        {
                            lstfact.Add(a);
                            break;
                        }
                    }
                    else
                    {
                        m = 0;
                    }
                   
                }
            }
               for (int k = lstfact.Count - 1; k >= 0;k-- )
               {
                   lstresult.Add(lstfact[k]);
               }
               return lstresult;
           }

    2.通过字符串存储

     private string factss(int n)
            {
                int s = 1;
                int m = 0;
                int a, b, c;
                StringBuilder strb = new StringBuilder();
                StringBuilder strfac = new StringBuilder();
                strb.Append("1");
                for (int i = 1; i <= n; i++)
                {
                    m = 0;
                    for (int k =0; k<strb.Length; k++)
                    {
                        s = i * int.Parse(strb[k].ToString());
                        s = s + m;
                        a = s / 10;
                        b = s % 10;
                        strb[k] =Convert.ToChar(b.ToString());
                        if (a > 0)
                        {
                            if (k < strb.Length - 1)
                            {
                                m = a;
                            }
                            else
                            {
                                strb.Append(a);
                                break;
                            }
                        }
                        else
                        {
                            m = 0;
                        }

                    }
                }
                for (int k =strb.Length-1; k >= 0; k--)
                {
                    strfac.Append(strb[k]);
                }
                return strfac.ToString();
            }

  • 相关阅读:
    [hihocoder-1974] 智能分包 状态压缩dp
    2018北京ICPC D. Frog and Portal 斐波那契数 构造
    [hdu-6621]K-th Closest Distance 主席树 线段树 2019 多校4
    [POJ 2104]K-th Number 主席树 可持久化线段树 入门
    [hdu-6623]Minimal Power of Prime
    [hdu-6608] Fansblog 威尔逊定理 质数的密度分布 2019 多校 3
    [codeforces1000F] One Occurrence
    [python] 机器学习 卷积神经网络 用迁移学习实现人脸识别
    [python] 安装TensorFlow问题 解决Cannot uninstall 'wrapt'. It is a distutils installed project
    浅谈getResource方法
  • 原文地址:https://www.cnblogs.com/jinyuttt/p/2296496.html
Copyright © 2020-2023  润新知