基本信息
源码名称:C# 阶乘示例代码
源码大小:4.87KB
文件格式:.cs
开发语言:C#
更新时间:2015-09-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


class Program
    {

        static void Main(string[] args)
        {
            List<int> a = new List<int>();//起点
            a.Add(1);//定义100!

            List<int> c = new List<int>();//阶次数

            int gs = 0;//是否有大于10,如有就去了个位取出来
            List<int> b = new List<int>();//存储中间变量
            List<int> sums = new List<int>();//存储有两个位数时 存储单个位数结果


            for (int i = 1; i <= 99; i  )
            {
                c.Clear();
                c.Add(i % 10);//把阶乘分成数组
                if (i >= 10)
                {
                    c.Add(i / 10 % 10);
                }
                for (int j = 0; j < c.Count; j  )
                {
                    if (c[j] != 0)//不为0时计算
                    {
                        gs = getNum1(a, c, gs, b, j);

                    }
                    else//如果是0  把集合定义为单个值(0值)
                    {
                        b.Add(0);
                    }



                    if (c.Count == 1)//当阶乘值不大于10(是单位数时)
                    {
                        a.Clear();//直接把数组做为下次阶乘的对象
                        for (int d = 0; d < b.Count; d  )
                        {
                            a.Add(b[d]);
                        }
                        b.Clear();
                    }
                    if (c.Count > 1)//阶乘大于10
                    {
                        if (sums.Count > 0)//是否为以有存在个位结果
                        {
                            a.Clear();
                            a.Add(b[0]);//增加最后的个位
                            int o = 0;//临时存储用于是否有进位数
                            int ss = 0;//计算结果
                            getNum2(a, b, sums, j, ref o, ref ss);
                            if (o != 0)//如果进位后没有就要加上进位的值
                            {
                                a.Add(o);
                            }
                            sums.Clear();
                            b.Clear();
                        }
                        else//第一次进行个位计算时把值增加到位结果集合中
                        {
                            sums.Clear();
                            for (int d = 0; d < b.Count; d  )
                            {
                                sums.Add(b[d]);
                            }
                            b.Clear();
                        }

                    }

                }

            }


            for (int s = a.Count - 1; s >= 0; s--)
            {
                Console.Write(a[s]);
            }
            Console.Read();
        }

        private static int getNum1(List<int> a, List<int> c, int gs, List<int> b, int j)
        {
            for (int k = 0; k < a.Count; k  )
            {
                int sum = 0;
                sum = c[j] * a[k];//用阶乘的值与目标值相乘
                if (gs != 0)//结果如大于10就要取出个位存到b集合中 并把去了个位上的数后把值存在进位变量中
                {
                    sum = sum   gs;
                }
                if (sum >= 10)
                {
                    int a1 = sum / 10 % 10;
                    int a2 = sum / 100 % 10;
                    gs = (a2 * 10)   a1;
                }
                else
                {
                    gs = 0;
                }
                b.Add(sum % 10);

            }
            if (gs != 0)//如是最大的时就把进位的值也加到集合中去
            {
                if (gs < 10)
                {
                    b.Add(gs);
                }
                else
                {
                    b.Add(gs % 10);
                    b.Add(gs / 10 % 10);
                }
                gs = 0;
            }
            return gs;
        }

        private static void getNum2(List<int> a, List<int> b, List<int> sums, int j, ref int o, ref int ss)
        {
            for (int d = 0; d < b.Count; d  )
            {
                if (d < sums.Count - 1)
                {
                    if (sums.Count > 1)
                    {
                        ss = sums[d   j]   b[d]   o;//加上进位数
                        o = 0;
                        if (ss >= 10)
                        {
                            o = 1;
                        }
                        else
                        {
                            o = 0;
                        }

                        a.Add(ss % 10);
                    }
                    else
                    {
                        a.Add(sums[d]   o);
                        o = 0;
                    }
                }
                else
                {
                    a.Add(b[d]   o);
                    o = 0;
                }
            }
        }


    }