具体代码自己想吧......^ ^ 不明白。溢出问题如何解决? 如果不嫌慢的话,既然不用long、int之类的语句来定义数据(显然不够长),我认为可以用一个数组来储存数据的各个位(个、十、百、千、万.....),再写一个函数来模拟程序储存数据的过程。 概率论里有一个斯特林公式:
n!≈Sqrt((2*π*n)*(n/e)^n)
n很大,且精确度要求不高时可以一试。
π=3.1415926535897……
e=2.7182818283…… 高精度计算速度比较慢的,可以实现
数字实在是太大了 找了段代码
/**///////////////////////////////////////////////////////////////////////////
// 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);
} 循环+高精 Ac 好长....... 编出来机子用得上吗
如何计算超大N的阶乘,N<100000 C;C++
如何计算超大N的阶乘,N<100000C;C++都可以
页:
[1]