N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 60244 Accepted Submission(s): 17166
Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
1
2
3
2
3
Sample Output
1
2
6
2
6
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
int a[];
int main()
{
int n;
int i,j;
while(~scanf("%d",&n))
{
if(n==)
{
printf("1\n");
continue;
}
memset(a,,sizeof(a));
a[]=;
for(i=;i<=n;i++)
{
for(j=;j<;j++)
a[j]*=i;
for(j=;j<;j++)
{
a[j+]+=a[j]/;
a[j]%=;
}
}
for(i=;i>=;i--)
{
if(a[i]!=)
break;
}
printf("%d",a[i--]);
for(;i>=;i--)
printf("%05d",a[i]);
printf("\n");
}
return ;
}
-----------------------------------------------
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
int a[];
int main()
{
//freopen("in.txt","r",stdin);
int n;
int i,j,temp;
while(~scanf("%d",&n))
{
memset(a,,sizeof(a));
a[]=;
int count=;
for(i=;i<=n;i++)
{
int k=;
for(j=;j<count;j++)
{
temp = a[j]*i+k;
a[j]=temp%;
k=temp/;
}
while(k)
{
a[count++]=k%;
k/=;
}
}
for(i=;i>=;i--)
{
if(a[i]!=)
break;
}
for(;i>=;i--)
printf("%d",a[i]);
printf("\n");
}
return ;
}
模板:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <ctype.h>
#include <map>
#include <string>
#include <set>
#include <bitset>
#include <utility>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <iostream>
#include <fstream>
#include <list>
using namespace std; const int MAXL = ;
struct BigNum
{
int num[MAXL];
int len;
BigNum()
{
memset(num,,sizeof(num));
}
}; //高精度比较 a > b return 1, a == b return 0; a < b return -1;
int Comp(BigNum &a, BigNum &b)
{
int i;
if(a.len != b.len) return (a.len > b.len) ? : -;
for(i = a.len-; i >= ; i--)
if(a.num[i] != b.num[i]) return (a.num[i] > b.num[i]) ? : -;
return ;
} //高精度加法
BigNum Add(BigNum &a, BigNum &b)
{
BigNum c;
int i, len;
len = (a.len > b.len) ? a.len : b.len;
memset(c.num, , sizeof(c.num));
for(i = ; i < len; i++)
{
c.num[i] += (a.num[i]+b.num[i]);
if(c.num[i] >= )
{
c.num[i+]++;
c.num[i] -= ;
}
}
if(c.num[len])
len++;
c.len = len;
return c;
}
//高精度乘以低精度,当b很大时可能会发生溢出int范围,具体情况具体分析
//如果b很大可以考虑把b看成高精度
BigNum Mul1(BigNum &a, int &b)
{
BigNum c;
int i, len;
len = a.len;
memset(c.num, , sizeof(c.num));
//乘以0,直接返回0
if(b == )
{
c.len = ;
return c;
}
for(i = ; i < len; i++)
{
c.num[i] += (a.num[i]*b);
if(c.num[i] >= )
{
c.num[i+] = c.num[i]/;
c.num[i] %= ;
}
}
while(c.num[len] > )
{
c.num[len+] = c.num[len]/;
c.num[len++] %= ;
}
c.len = len;
return c;
} //高精度乘以高精度,注意要及时进位,否则肯能会引起溢出,但这样会增加算法的复杂度,
//如果确定不会发生溢出, 可以将里面的while改成if
BigNum Mul2(BigNum &a, BigNum &b)
{
int i, j, len = ;
BigNum c;
memset(c.num, , sizeof(c.num));
for(i = ; i < a.len; i++)
{
for(j = ; j < b.len; j++)
{
c.num[i+j] += (a.num[i]*b.num[j]);
if(c.num[i+j] >= )
{
c.num[i+j+] += c.num[i+j]/;
c.num[i+j] %= ;
}
}
}
len = a.len+b.len-;
while(c.num[len-] == && len > )
len--;
if(c.num[len])
len++;
c.len = len;
return c;
}
void print(BigNum &a) //输出大数
{
int i;
for(i = a.len-; i >= ; i--)
printf("%d", a.num[i]);
puts("");
} void Init(BigNum &a, char *s, int &tag) //将字符串转化为大数
{
int i = , j = strlen(s);
if(s[] == '-')
{
j--;
i++;
tag *= -;
}
a.len = j;
for(; s[i] != '\0'; i++, j--)
a.num[j-] = s[i]-'';
} int main(void)
{
int n;
while(~scanf("%d",&n))
{
BigNum a;
int tag=;
Init(a,"",tag);
for(int i=;i<=n;i++)
{
a=Mul1(a,i);
}
print(a);
}
return ;
}