Isword 发表于 2007-4-26 13:00:42

就是一个递归算法应该可以搞定了......
具体代码自己想吧......^ ^

cgx1235 发表于 2007-4-30 05:20:10

不明白。溢出问题如何解决?

windybell 发表于 2007-4-30 22:39:15

如果不嫌慢的话,既然不用long、int之类的语句来定义数据(显然不够长),我认为可以用一个数组来储存数据的各个位(个、十、百、千、万.....),再写一个函数来模拟程序储存数据的过程。

dragon_ 发表于 2007-5-1 16:59:15

概率论里有一个斯特林公式:
n!≈Sqrt((2*π*n)*(n/e)^n)
n很大,且精确度要求不高时可以一试。
π=3.1415926535897……
e=2.7182818283……

红色激情 发表于 2007-5-1 19:39:35

songfei 发表于 2007-5-1 21:22:38

高精度计算速度比较慢的,可以实现

数字实在是太大了

songfei 发表于 2007-5-1 21:29:05

找了段代码


/**///////////////////////////////////////////////////////////////////////////
//    Date created:   2005/07/12
//    Author:      Confach Zhang
//    Purpose:      计算n!的值
/**///////////////////////////////////////////////////////////////////////////

   
using namespace std;
#include "StdAfx.h"
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <iomanip.h>

int GetNumber();                        //输入 n
int GetBitLength(int n);                  //求n!的位数
char* Initialize(int);                  //初始化存储结果的值
void PrintValue(char *a,int size);            //打印值到屏幕
void PrintValue(char *a,int size,char* fileName);//打印值到文件
char* GetValue(int val);                  //计算
char* SubGetValue(char* ,int);               


int main()
{
    int value=GetNumber();
    char fileName;
    int size=GetBitLength(value);
    char *pa = Initialize(size);

    //pa=GetValue();
    pa=GetValue(value);

    PrintValue(pa,size);

    //sprintf(fileName,"%s","10000!.txt");
    sprintf(fileName,"%d!.txt",value);
   
    PrintValue(pa,size,fileName);
    delete []pa;//note:
    return 1;
}
//函数GetValue
// 求得计算结果
//返回结果
//History:
//1)char* GetValue()
//2)GetValue(int val)
//参数:val 计算阶乘的值
char* GetValue(int val)
{
    //定义一个数组存储阶乘的值
    //首先得到10000!阶乘的位数
    int VALUE=val;
    int length=GetBitLength(VALUE);
    char *arrValue = new char;
    if(!arrValue) {
      cout <<"申请内存失败!" << endl;
      exit(1);
    }
    arrValue = 1;
    for(int i=1; i<length; i++)
      arrValue = 0;
    arrValue=SubGetValue(arrValue,VALUE);
    return arrValue;
}

char* SubGetValue(char* arrValue,int n)
{
    int index=0;
    long carrier=0;
    double bitCount = 1;
    int begin = 0;

    for(index=2; index<=n; ++index)
    {
      long multiValue = 0;
      bitCount += log10((long double)index);
      if(arrValue == 0)
         begin++;

      for(int j=begin; j<int(bitCount); ++j)
      {
         multiValue += (index*arrValue);
         arrValue = char(multiValue % 10);
         multiValue /= 10;
      }
    }
return arrValue;
}

//得到计算阶乘的值,此函数为新增
int GetNumber()
{
    int n;
    cout << "请输入要计算阶乘的n值: ";
   cin >> n;
   while(n < 0) {
      cout << "输入错误,请重新输入: ";
      cin >> n;
   }
   if(n == 0)
      exit(1);
   return n;
}

//函数GetBitLength
// 求得计算结果的位数,本函数为新增加
//参数
//    n 需要计算的阶乘的数
//返回结果的位数
int GetBitLength(int n)
{
   double sum = 1.0;
   for(int i=1; i<=n; i++)
      sum += log10((long double)i);
   return int(sum);
}

//-----------
//函数:Initialize
//初始化存储结果的数组
//参数:
//    size    数组的长度
//返回值
//   初始化后的数组
//-------------
char * Initialize(int size)
{
   char *arrValue = new char;
   if(!arrValue) {
      cout << size<<"太大,申请内存失败!" << endl;
      exit(1);
   }
   arrValue = 1;
   for(int i=1; i<size; i++)
      arrValue = 0;
   return arrValue;
}

//-----------
//函数:PrintValue
//将结果输入到屏幕上
//参数:
//    buff    存储结果的数组
//buffLen数组的长度
//fileName文件名      
//-------------
void PrintValue(char *buff, int buffLen)
{
   int bit = 0;
   int nCol=0;
   for(int i=buffLen-1; i>=0; i--) {
      if(bit % 10 == 0)
      {
      cout << " " ;
      nCol++;
      if(nCol==10)cout<<endl;
      }
       cout << int (buff);
      bit++;
   }
   cout << endl;
   
}
//-----------
//函数:PrintValue
//将结果输入到一个文件中
//参数:
//    buff    存储结果的数组
//buffLen数组的长度
//fileName文件名      
//-------------

void PrintValue(char *buff,int buffLen,char *fileName)
{
    int bit = 0;
   int nCol=0;

   FILE *fp=NULL;
   //-----------------------------

   if (fileName==NULL)      return ;
fp=fopen(fileName,"wt");
   if (fp==NULL)
   {
      printf("不能创建文件%s",fileName);
      return ;
   }

   for(int i=buffLen-1; i>=0; i--)
   {
      fprintf(fp,"%d",int(buff));
      
      if(bit % 9 == 0)
      {
      fprintf(fp,"%s"," ");
      nCol++;
      if(nCol==8)
      {
          fprintf(fp,"%s","\n");
          nCol=0;
      }
      }
      bit++;
      
}
   fprintf(fp,"\n");
   fclose(fp);
}

jason911 发表于 2007-6-5 18:37:25

循环+高精 Ac

零点 发表于 2007-6-5 22:26:00

好长.......

零点 发表于 2007-6-5 22:28:55

编出来机子用得上吗

红色激情 发表于 2007-6-5 22:30:41

cgx1235 发表于 2007-4-17 11:33:37

如何计算超大N的阶乘,N&lt;100000 &nbsp; C;C++

如何计算超大N的阶乘,N<100000

C;C++都可以
页: [1]
查看完整版本: 如何计算超大N的阶乘,N&lt;100000 &nbsp; C;C++