求编写一个求100的阶乘的程序。

求编写一个求100的阶乘的程序。,第1张

#include <凳察悔stdio.h>没蔽

#define N 100//计算10的阶乘,枣正最多只能算到9999 long a[8916]int main(int argc, char* argv[])

{

long n,i,c,len

a[0]=1

len=1

for( n=Nn>1n--)

{

for(c=0,i=0i<leni++)

{

long p=a[i]*n+c

a[i]=p%10000

c=p/10000

}

a[i]= c

if(c>0)

{

len++

}

} printf("%d",a[len-1])

for(i=len-2i>=0i--)

{

printf("%04d",a[i])

}

printf("\n")

return 0

}

如果要近似的,可以这么做:

//#include "stdafx.h"//If the vc++6.0, with this line.

#include <iostream>

using namespace std

int main(int argc,char *argv[]){

    int i

    double s

    for(s=i=1i<101s*=i++)

    cout << "100! = " << s << endl

    return 0

}

如果要精确的,就得麻烦点:

//#include "stdafx.h"//If the vc++6.0, with this line.

#include <iostream>

#include "math.h"

using namespace std

int main(int argc,char *argv[]){

    int i,j,n,*p

    double x

    for(x=0.0,i=2i<101x+=log10(i++))

    if((p=new int[x/=6,n=(int)x+(x!=(int)x)])==NULL){

        cout << "Application memory failure...\n"

        滑坦return 0

    }

    for(i=0i<np[i++]=0)

    for(p[n-1]=1,i=2i<101i++){

        for(j=0p[j]==0j++)

        while(j<n)

            p[j++]*=i

        for(j--j>0j--)

            if(p[j]>999999)

       首让山         者中p[j-1]+=p[j]/1000000,p[j]%=1000000

    }

    cout << "100! = " << p[0]

    cout.fill('0')

    for(i=1i<ncout.width(6),cout << p[i++])

    delete []p

    cout << endl

    return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12479814.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存