C编程题(40)

 一个整型变量只能用来存贮较小的 N!的值,当 N 较大时,可将阶乘值中的 每一个数字放在一个一维数组的一个元素中。使用这方法,打印:
    ① N!的值;
    ② N!-M!(M>N);
    ③ N!+M! 

 

[C#]

 

        public const int max = 30;
        public static  int N,M;

        public static void Main(String[] args)
        {      
            Console.Write("Please input N:");
            N = Convert.ToInt32(Console.ReadLine());
            if (N < 1)
            {
                Console.WriteLine("输入有误!");
                Console.ReadLine();
                return;
            }

            Console.Write("Please input M:");
            M = Convert.ToInt32(Console.ReadLine());
            if (M <1 || M>=N)
            {
                Console.WriteLine("输入有误!");
                Console.ReadLine();
                return;
            }

            int[] a = Factorial(N);
            int[] b = Factorial(M);
            Console.WriteLine("N!="+Print(a));
            Console.WriteLine("M!=" + Print(b));

            Console.WriteLine("N!+M!=" + Print(FacAdd(a,b)));
            Console.WriteLine("N!-M!=" + Print(FacSubtract(a,b)));

            int x=Factorial(N,false);
            int y=Factorial(M, false);
            Console.WriteLine("N!={0}", x);
            Console.WriteLine("M!={0}", y);
            Console.WriteLine("N!+M!={0}", x+y);
            Console.WriteLine("N!-M!={0}", x-y);

            Console.ReadLine();
        }

        public static int Factorial(int n,bool flag)
        {
            if (n == 1)
                return 1;
            else
                return n * Factorial(n - 1,false);
        }

        public static int[] Factorial(int n)
        {
            int[] fa = new int[max];
            fa[0] = 1;
            int r;
            for (int i = 1; i <= n; i++)
            {
                r = 0;
                for (int j = 0; j < max; j++)
                {
                    fa[j] = fa[j] * i + r;
                    r = fa[j] / 10;
                    fa[j] %= 10;
                }
            }
            return fa;
        }

        public static int[] FacAdd(int[] a, int[] b)
        {
            int tmmax = Math.Max(a.Length, b.Length);
            int[] res = new int[tmmax];
            int r=0;
            for (int i = 0; i < tmmax; i++)
            {
                if(i<a.Length)
                    r += a[i];
                if (i < b.Length)
                    r += b[i];

                res[i] = r % 10;
                r /= 10;
            }
            return res;
        }

        public static int[] FacSubtract(int[] a, int[] b)
        {
            int tmmax = Math.Max(a.Length, b.Length);
            int[] res = new int[tmmax];
            int r = 0;
            for (int i = 0; i < tmmax; i++)
            {
                if (i < a.Length)
                    r += a[i];
                if (i < b.Length)
                    r -= b[i];
                if (r < 0)
                {
                    res[i] = r + 10;
                    r = -1;
                }
                else
                {
                    res[i] = r;
                    r = 0;
                }
            }
            return res;
        }

        public static string Print(int[] fac)
        {
            int i = fac.Length-1;
            string sfac = string.Empty;
            while (i >= 0 && fac[i] == 0)
                i--;
            while (i >= 0)
                sfac += Convert.ToString(fac[i--]).Trim();
            return sfac;
        }

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值