poj 1423 Big Number
来源:互联网 发布:环形队列 c语言 编辑:程序博客网 时间:2024/05/02 06:46
题意:给出一个数字N,求N!的结果的位数。
首先要求一个数字有多少位,可以用(int)log10(num)+1,这样就求出num有多少位.
数N可以到10^7这么大,直接暴力的话 肯定超时。
考虑下面两种方法,个人比较推荐第二种。
第一种是直接打表,避免重复计算,如果要追求速度的话,可以在程序外打表,然后导入,在程序中直接查,即可,那样肯定0MS,下面的代码是在程序中打表的,所以时间爱你耗费较多。
第二种是利用Stirling公式. n!≈sqrt(2*pi*n)*[(n/e)^n]
n越大精确度越高,这里只要计算位数,所以是可以用的。
下面是两个算法的代码
首先要求一个数字有多少位,可以用(int)log10(num)+1,这样就求出num有多少位.
数N可以到10^7这么大,直接暴力的话 肯定超时。
考虑下面两种方法,个人比较推荐第二种。
第一种是直接打表,避免重复计算,如果要追求速度的话,可以在程序外打表,然后导入,在程序中直接查,即可,那样肯定0MS,下面的代码是在程序中打表的,所以时间爱你耗费较多。
第二种是利用Stirling公式. n!≈sqrt(2*pi*n)*[(n/e)^n]
n越大精确度越高,这里只要计算位数,所以是可以用的。
下面是两个算法的代码
#include<stdio.h> #include<math.h> int num[10000001]; main() { int n,i,t; double d; for(i=1,d=0;i<10000001;i++) { d+=log10(i); num[i]=(int)d+1; } for(scanf("%d",&t);t>0;t--) { scanf("%d",&n); printf("%d\n",num[n]); } } #include<iostream> #include<cmath> using namespace std; const double e = 2.7182818284590452354, pi = 3.141592653589793239; double strling_digits_num(int n) { return log10(2*pi*n)/2.0+n*(log10(n/e)); } int main() { int t; cin>>t; while(t--) { int n; cin>>n; double m=0; m=strling_digits_num(n); int answer=(int)m; answer++; cout<<answer<<endl; } return 0; }
- POJ 1423 Big Number
- POJ 1423 Big Number
- POJ 1423 Big Number
- poj 1423 Big Number
- poj 1423 big number
- POJ 1423 Big Number
- POJ 1423 Big Number
- poj 1423 Big Number
- big number poj 1423
- POJ 1423 Big Number
- POJ 1423 Big Number
- poj - 1423-Big number
- POJ:1423 Big Number
- poj 1423 Big Number
- POJ 1423 Big Number
- 【poj】1423 Big number
- POJ 1423 BIG NUMBER
- POJ 1423 Big Number
- mysql中的内连接,外连接实例详解
- Android处理图片OOM的若干方法小结
- 公司员工没有管理员权限怎么办 C#文件夹、文件添加权限 C#读取windows系统日志 C#设置环境变量
- OSI模型
- setsockopt函数使用
- poj 1423 Big Number
- static 使用
- {{JS}}无提示强X当前窗口
- 什么是真实的?
- PHP 帕斯卡(Pascal)三角形
- C#高级程序设计(二)——代理基础
- Windows下修改android 模拟机 hosts文件
- 栅格数据入库
- gdb调试WebCore