正文

Number lengths2007-04-27 10:57:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/xiaoxiaofeng/25269.html

分享到:

  Number lengths N! (N factorial) can be quite irritating and difficult to compute for large values of N. So instead of calculating N!, I want to know how many digits are in it. (Remember that N! = N * (N - 1) * (N - 2) * ... * 2 * 1) Input: Each line of the input will have a single integer N on it 0 < N < 1000000 (1 million). Input is terminated by end of file. Output: For each value of N, print out how many digits are in N!. Sample Input: 1 3 32000 Sample Output: 1 1 130271 /* n!=1*2*3*....*n           →         lg(n!)=lg(1*2*3*....*n)=lg(1)+lg(2)+   lg(3)+..+lg(n)           →         n!=10^(lg(1)+lg(2)+   lg(3)+..+lg(n))   */ #include<iostream> #include<cmath> using namespace std; int main() {  long n,i;  double sum;  long temp;  while(scanf("%d",&n)!=EOF)  {   sum=0.0;   for(i=1;i<=n;i++)sum+=log10((double)i);   temp=long(sum+1);   cout<<temp<<endl;  }  return 0; }

阅读(279) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册