| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2531 人关注过本帖
标题:如何求一个较大的数的阶乘啊?有的数的阶乘太大一运行程序得到的结果都错了 ...
只看楼主 加入收藏
ZZlove
Rank: 2
等 级:论坛游民
帖 子:29
专家分:28
注 册:2010-3-7
结帖率:25%
收藏
已结贴  问题点数:20 回复次数:7 
如何求一个较大的数的阶乘啊?有的数的阶乘太大一运行程序得到的结果都错了。
我用递归写了一个,但只能得到小于15的数的阶乘。看书上说要用一个数组来表示太大的结果,还有什么大整数乘法,求教。
这个是用递归写的
#include<stdio.h>
long int fn(int a);
void main()
{
 int n;
 long int sum;
 scanf("%d",&n);
 sum = fn(n);
 printf("%d\n",sum);
}
long int fn(int a)
{
 if(a == 1)
 {
  return 1;
 }
 else
 {
  a = a*fn(a-1);
  return a;
 }
}
搜索更多相关主题的帖子: 阶乘 结果 运行 
2010-06-03 23:15
zhuxu0423
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:59
专家分:101
注 册:2010-4-12
收藏
得分:4 
#include "stdio.h "
#include "malloc.h "
#define   MAXN   10000
void   pnext(int   a[],int   k)
{
  int   *b,m=a[0],i,j,r,carry;
  b=(int*)malloc(sizeof(int)*(m+1));
  for(i=1;i <=m;i++)   b[i]=a[i];
  for(j=1;j <k;j++){
    for(carry=0,i=1;i <=m;i++){
      r=(i <=a[0]?a[i]+b[i]:a[i])+carry;
      a[i]=r%10;
      carry=r/10;
      }
      if(carry)a[++m]=carry;
    }
    free(b);
    a[0]=m;
}
void   write(int   *a,int   k)
{
    int   i;


    printf( "%4d!= ",k);
    for(i=a[0];i> 0;i--)
      printf( "%d ",a[i]);

    printf( "\n\n ");
}
void   print(int   *a,   int   k)
{
    int   i;
    FILE   *fp;
    fp=fopen( "fun.txt ", "a ");
    for(i=a[0];i> 0;i--)
    fprintf(fp, "%d ",a[i]);
    fclose(fp);
}
void   main()
{
  int   a[MAXN],n,k;
  printf( "Enter   the   number   n:   ");
  scanf( "%d ",&n);
  a[0]=1;a[1]=1;write(a,1);
  for(k=2;k <=n;k++){
    pnext(a,k);
    write(a,k);
    }
  print(a,k);
}
2010-06-04 18:35
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:4 
可以用long 类型longlong 类型,

2010-06-04 18:47
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:0 
回复 2楼 zhuxu0423
你的阶乘体现在哪里?还有你的程序思想能讲一下么,因为我不确定我的理解是你的意思
2010-06-04 19:16
ZZlove
Rank: 2
等 级:论坛游民
帖 子:29
专家分:28
注 册:2010-3-7
收藏
得分:0 
回复 2楼 zhuxu0423
你用的是一个数组来存这个较大的数吧,能解释一下吗?我新手有的看不懂。
2010-06-04 23:22
ipodqiu
Rank: 2
等 级:论坛游民
帖 子:9
专家分:16
注 册:2010-5-30
收藏
得分:4 
求较大数阶乘时,输出时要出:%e
2010-06-05 08:39
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:4 
程序代码:
#include<iostream>
#include<vector>
#include<string>
#include<iterator>
#include<cassert>
#include<ostream>
class BigInt
{
public:
    explicit BigInt(int i);
    BigInt(BigInt &other);
    ~BigInt();
    BigInt & operator + (BigInt &);
    BigInt & operator * ( int );
    BigInt & operator = (const BigInt &);
    friend std::ostream & operator << ( std::ostream & out, BigInt &bg);
    std::vector<int> & getdt() {return dt;}
private:
    const int Base;
    std::vector<int> dt;
};

BigInt::BigInt(int i=0):Base(10)
{
    assert(dt.empty());
    for ( ; i > 0; i /= Base)
    {
    dt.push_back(i%Base);
    }
}
BigInt::BigInt(BigInt &other):Base(10)
{
    assert(this->dt.empty());
    this->dt=std::vector<int>(other.getdt());
}

BigInt & BigInt::operator = (const BigInt & other)
{
    this->dt=other.dt;    
    return *this;
}

BigInt::~BigInt()
{
    dt.clear();
}


std::ostream & operator << ( std::ostream & out, BigInt &bg)
{
    if( 0 == bg.dt.size())
    out<<int(0);
    std::vector<int>::const_reverse_iterator rit = bg.dt.rbegin();
    for( ; rit != bg.dt.rend() ; rit++ ) 
    {
    out<<*rit;
    } 
    return out;
}

BigInt& BigInt::operator+ ( BigInt & other)
{
    int carry=0;
    const std::size_t len=this->dt.size();
    std::vector<int>::iterator it,jt;
    for( it = dt.begin(),jt = other.dt.begin(); it < dt.end() || jt < other.dt.end() || carry > 0 ; )
    {
    if( it < dt.end() && jt < other.dt.end() )
    {
        carry += * it + *jt ;
        *it = carry % Base;
        carry /= Base;
        it ++;
        jt ++;
    }
    else if ( it < dt.end() )
    {
        carry += *it;
        *it = carry % Base;
        carry /= Base;
        it ++;
    }
    else
    {
        carry += *jt;
        dt.push_back(carry % Base);
        carry /= Base;
        jt++;
    }
    if ( carry > 0 )
    {
        dt.push_back(carry);
    }
    }
    return *this;
}

BigInt & BigInt::operator * (int other)
{
    int carry=0;
    if ( 0 == other )
    {
    this->dt.clear();
    dt.push_back(0);
    return *this;
    }
    std::vector<int>::iterator it;
    for( it = dt.begin(); it != dt.end(); it ++ )
    {
    carry += *it* other;
    *it= carry % Base;
    carry /= Base;
    }
    if ( carry > 0 )
    {
    dt.push_back(carry);
    }
    return *this;
}


BigInt factorial(int other)
{
    BigInt ret(1);
    assert( other >= 0 );
    if( other == 0 )
    return ret;
    for( int i=1 ; i <= other ; i++ )
    {
    ret*i;
    }
    return ret;
}


int main()
{
    BigInt ret;
    int m;
    std::cin>>m;
    ret=factorial(m);
    std::cout<<ret<<std::endl;
    return 0;
}


[ 本帖最后由 Devil_W 于 2010-6-5 17:11 编辑 ]
2010-06-05 15:29
ppfly
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:297
专家分:1956
注 册:2009-5-17
收藏
得分:4 
回复 楼主 ZZlove
程序代码:
#include <stdio.h>
void fun(int n);
int a[100000]={0};
unsigned LengTh=0;
int main()
{
    int i,n;
    while(scanf("%d",&n)!=EOF)
    {
        fun(n);
        i=LengTh;
        printf("%d",a[i]);
        i--;
        while(i>=0)
        {
            printf("%05d",a[i]);
            i--;
        }
        printf("\n");
    } 
    return 0;
}
void fun(int n)
{
    int i;
    unsigned long c,m;
    LengTh=0;
    if(n==1 || n==0)
    {
        a[0]=1;
        LengTh=0;
        return;
    }
    if(n>=2)
    {
        fun(n-1);
        c=0;
        for(i=0;i<=LengTh;i++)
        {
            m=a[i]*n;
            a[i]=(m+c)%100000;
            c=(m+c)/100000;
        }
        while(c)
        {
             a[i]=c%100000;
             i++;
             LengTh++;
             c=c/100000;
        }
    }
}

********多贴代码,少说空话*******
2010-06-06 02:07
快速回复:如何求一个较大的数的阶乘啊?有的数的阶乘太大一运行程序得到的结果都 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019494 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved