Длинная арифметика (1!+2!+...+100!) в десятичной системе счисления. - C#

Узнай цену своей работы

Формулировка задачи:

Ребят делаю курсовую вот и получилось,что не могу отобразить большое число (1!+2!+...+100!). Как это можно написать через длинную арифметику?

Решение задачи: «Длинная арифметика (1!+2!+...+100!) в десятичной системе счисления.»

textual
Листинг программы
/*
 * Created by SharpDevelop.
 * User: M128K145
 * Date: 04.12.2009
 * Time: 17:03
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
 
using System;
 
namespace Factorials
{
    /// <summary>
    /// Description of Factorial.
    /// </summary>
    public class Factorial
    {
        public Factorial()
        {
            mas = new int[n];
        }
        private const int n = 1000000;
        private int[] mas;
 
        public string PrintFactorial(int factorial)
        {
            _Factorial(factorial);
            string rezult = "";
            int i = 0, z = n - 1;
 
            while (mas[i++] == 0) ;
 
            for (i -= 1; i < n; ++i)
            {
                if ((mas[i] > 0) || (i > z))
                    z = i;
                if (mas[i] >= 100)
                    rezult += mas[i] + " ";
                else
                    if (mas[i] < 10)
                        rezult += "00" + mas[i] + " ";
                    else
                        if (mas[i] < 100)
                            rezult += "0" + mas[i] + " ";
            }
            return rezult;
        }
 
        private void _Factorial(int factorial)
        {
            int fact, i, zero = n - 1, index = n - 1;
            for (i = 0; i < n - 1; ++i)
                mas[i] = 0;
            mas[n - 1] = 1;
            for (fact = 2; fact <= factorial; ++fact)
            {
                while (mas[zero] == 0) zero--;
                for (i = zero; i >= index; --i)
                    mas[i] *= fact;
 
                for (i = zero; i > index; --i)
                    if (mas[i] >= 1000)
                    {
                        mas[i - 1] += mas[i] / 1000;
                        mas[i] %= 1000;
                    }
 
                if (mas[index] >= 1000)
                {
                    mas[index - 1] += mas[index] / 1000;
                    mas[index] %= 1000;
                    index--;
 
                    if (mas[index] >= 1000)
                    {
                        mas[index - 1] += mas[index] / 1000;
                        mas[index] %= 1000;
                        index--;
                        if (mas[index] >= 1000)
                        {
                            mas[index - 1] += mas[index] / 1000;
                            mas[index] %= 1000;
                        }
                    }
                } 
            }
        }
    }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 3.9 из 5
Похожие ответы