大数的阶乘

include<stdio.h> //大数的阶乘比如1<n<25000

define MAX 1000

using namespace std;

int main()
{
int n;
while(scanf("%d",&n)==1&&n>=0)
{
int i,j;
int a[MAX]; //存数运算结果
int p,h; //p存储当前结果的位数,h为进位
a[1]=1;
p=1;
for(i=2;i<=n;i++) //循环与2,3,4.....n相乘
{
for(j=1,h=0;j<=p;j++) //让a[]的每位与i相乘
{
a[j]=a[j]*i+h;
h=a[j]/10;
a[j]=a[j]%10;
}
while(h>0) //如果h不为0,h值可能很大,j在上一个循环跳出是已经加了一
{
a[j]=h%10;
h=h/10;
j++;
}
p=j-1; //将当前的位数赋给p,j在上一个循环跳出是加了一,p标记位数现在减一
}
for(i=p;i>=2;i--) //逆序输出
{
printf("%d",a[i]);
}
printf("%d\n",a[i]); //估计只是为了\n所以单独写一个
}
return 0;
}

posted @ 2016-12-11 10:22  nn麦田  阅读(137)  评论(0编辑  收藏  举报