日期:2014-05-20  浏览次数:20966 次

求1000的阶乘
如何算出1000的阶乘

------解决方案--------------------
本想简单胡乱的测试一下,但花了很长时间
Java code

public class Test {
    static int[] v = new int[500];
    static int ten = 0;
    static int length = 1;

    public static void m(int t) {
        while (t%10==0) {
            t /= 10;
            ten++;
        }
        for (int i=0; i<length; i++) v[i]*=t;
        int value;
        for (int i=0; i<length; i++) {
            value = v[i];
            v[i] = 0;
            t = i;
            while (value>0) {
                if (length==t) length++;
                v[t] = v[t]+value%1000000;
                value /= 1000000;
                t++;
            }
        }
    }
    
    public static void n(int t) { for (int i=1; i<=t; i++) m(i); }

    public static void main(String[] args) {
        v[0] = 1;
        long time = System.currentTimeMillis();
        n(1000);
        System.out.println(System.currentTimeMillis()-time);

        int i=v.length-1;
        while (v[i]==0) i--;
        for (; i>-1; i--) {
            System.out.print(v[i]/100000);
            System.out.print(v[i]/10000%10);
            System.out.print(v[i]/1000%10);
            System.out.print(v[i]/100%10);
            System.out.print(v[i]/10%10);
            System.out.print(v[i]%10);
        }
        for (i=0; i<ten; i++) {
            System.out.print('0');
        }
    }
}

------解决方案--------------------
给出程序代码和输出结果,来这里是研究程序的,不是来吵架的.
Java code
class PowerOf1000
{

  public static void main(String[] args)
  {
    int[] digits = new int[2568];
    int max_digit = 2567;
    digits[max_digit] = 1;
    for (int d=2;d<=1000;d++)
    {
      for (int k=max_digit; k<digits.length; k++)
        digits[k] *= d;

      int k = digits.length-1;
      while (k>=max_digit)
      {
        if (digits[k]>10)
        {
          digits[k-1] += digits[k] / 10;
          digits[k] = digits[k] % 10;
          if (k-1<max_digit) max_digit = k-1;
        }
        k--;
      }
    }
    for (int i=max_digit; i<digits.length; i++)
    {
      System.out.print(digits[i]);
    }
    System.out.println();
  }
}

------解决方案--------------------
数值计算基础问题。给个计算1! + 2! + ... + n!的,O(n*n)
C/C++ code

//calculate 1! + 2! + ... + n! where n is a positive integer

#include <iostream>
using namespace std;
const long maxlen = 100000;

int main()
{
    cout << "Input n: ";
    int n = 0;
    cin >> n;
    cout << "1! + 2! + ... + " << n << "! = ";
    
    int result[maxlen] = {0}, len_r = 0;
    int temp[maxlen] = {0}, len_t = 0;
    
    temp[0] = 1;
    len_t = 1;
    
    for (int x = 1; x <= n; x++)
    {
        temp[0] = temp[0] * x;
        for (int j = 1; j < len_t; j++)
        {
            temp[j] = temp[j] * x + temp[j-1] / 10;
            temp[j-1] = temp[j-1] % 10;
        }
            
        while (temp[len_t-1] > 10)
        {
            temp[len_t] = temp[len_t-1] / 10;
            temp[len_t-1] = temp[len_t-1] % 10;
            len_t++;
        }
        
        for (int i = 0; i < len_t; i++)
        {
            result[i] = result[i] + temp[i];
            result[i+1] = result[i+1] + result[i] / 10;
            result[i] = result[i] % 10;
        }
        
        len_r = len_t;
        while (result[len_r] > 0)
        {
            result[len_r+1] = result[len_r] / 10;
            result[len_r] = result[len_r] % 10;
            len_r++;
        }
        
    }
    
    int k = len_r - 1;
    while (result[k] == 0 && k > 0) k--;
    for (; k >= 0; k--) cout << result[k];
    cout << endl << endl << "Total Digits: " << len_r << endl;
    return 0;
}

------解决方案--------------------