51Nod 1057 N的阶乘
输入N求N的阶乘的准确值。
Input
输入N(1 <= N <= 10000)
Output
输出N的阶乘
Input示例
5
Output示例
120
C语言:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <map> #include <set> #include <vector> #include <algorithm> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define PI 3.141592653589793238462 #define INF 0x3f3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; const int maxn=1e5+10; const int mod=100000000;//精度可以自己设置 ll a[maxn],n,ans,pos=0; int main() { scanf("%lld",&n); a[0]=1; for(int i=1;i<=n;i++) { ans=0; for(int j=0;j<=pos;j++) { a[j]=a[j]*i+ans; ans=a[j]/mod; a[j]%=mod; } if(ans>0) a[++pos]=ans; } printf("%lld",a[pos]); for(int i=pos-1;i>=0;i--) printf("%0.8lld",a[i]); printf("\n"); return 0; }
Java语言:
import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(System.in); int n; n=cin.nextInt(); BigInteger m=BigInteger.ONE; BigInteger a=BigInteger.valueOf(n); BigInteger b=BigInteger.ONE; for(BigInteger i=BigInteger.ONE;i.compareTo(a)<=0;i=i.add(b)) { m=m.multiply(i); } System.out.println(m); } }
51Nod 1058 求N阶乘长度
输入N求N的阶乘的10进制表示的长度。例如6! = 720,长度为3。
Input
输入N(1 <= N <= 10^6)
Output
输出N的阶乘的长度
Input示例
6
Output示例
//求N阶乘的位数 #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <map> #include <set> #include <vector> #include <algorithm> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define INF 0x3f3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; int main() { ll n; scanf("%lld",&n); ll ans=0.5*log10(2*pi*n)+n*log10(n*1.0/ei);//注+1不要跟在公式后面,否则1不成立 printf("%lld\n",ans+1); ll pos=log10((long double)(sqrt(2*n*pi)))+n*log10((long double)n/ei); printf("%lld\n",ans+1); long long l=log10((long double)(sqrt(2*M_PI*n)))+n*log10((long double)n/M_E); printf("%.8lf %.8lf\n",M_PI,M_E);//M_PI M_E 自带函数 printf("%lld\n",l+1); return 0; }
3