大数运算(续)
?? ACM競賽中另一個基礎運算,大數(shù)的階乘(factorial),其實階乘中要算超出int表示范圍的階乘這是個十分浩瀚的工程,其實就是很多個大數(shù)先乎乘積再求和。
?? ?真不敢想象一個位數(shù)超過百位的數(shù)字,它的階乘,這個目前不考率,^_^,這樣的話就大大降低了復雜度和難度。
?? 這個無非就是大數(shù)相乘的思路了。廢話不多說,上代碼。我的這個效率屬于比較低的那種,也是模擬手算過程的程序化。
code:
#include<cstdio>
#include<cstdlib>
const int Max = 500;
int rslt[Max];
void Factorial(int n)
{
rslt[0] = rslt[1] = 1;
int indx;
if(n==1||n==0)
return;
for(int i=2;i<=n;i++)
{
indx = 0;
? ? ? int pre = 0,j; //the symbal of carry set
for( j=1;j<=rslt[0];j++)
{
rslt[j] *=i;
? ? ? ?rslt[j] += pre;?
pre = rslt[j]/10;
rslt[j] %= 10;
}
while(pre) ? //process the last byte?
{
rslt[j] = pre%10;
rslt[0] = j;
pre /= 10;
j++;
}
?
}
for(int Ick=rslt[0];Ick>=1;Ick--)
printf("%d",rslt[Ick]);
?? ? ? ?printf("\n");
}
void Init()
{
for(int i=0;i<Max;i++)
rslt[i] = 0;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
Init();
Factorial(n);
}
return 0;
}
轉載于:https://blog.51cto.com/jeick/544374
總結
- 上一篇: 【转】 看IT人才最容易犯的17个错误
- 下一篇: 【注册机】ColorSchemer St