zoukankan      html  css  js  c++  java
  • POJ 1423 Big Number

    Big Number

    Description

    In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.


    Input

    Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 <= m <= 10^7 on each line.


    Output

    The output contains the number of digits in the factorial of the integers appearing in the input.


    Sample Input

    2
    10
    20


    Sample Output

    7
    19

    这题也是利用log10这个函数,和POJ 1019一样,这不过在这里,如果对每个数单独求值,则会超出时间。因而我们采用了以空间来换时间的策略,将8000001个数先计算出来保存在内存中,对于在这个范围的数,则直接取出,超出这个范围的,则进一步计算。

    具体代码如下:

     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <time.h>
     4 
     5 //log10(n!)求的就是n!的位数,然后展开之
     6 #define LEN 8000001
     7 double result[LEN];
     8 int count(int n)
     9 {
    10     double var = 0.0;
    11     //clock_t start = clock();
    12     int i;
    13     for(i = LEN; i <= n; i++)
    14         var += log10((double)i);
    15     return (int)(result[LEN - 1] + var) + 1;
    16     //clock_t end = clock();
    17     //printf("%f", (double)(end -start)/CLOCKS_PER_SEC);
    18     
    19 }
    20 
    21 int main()
    22 {
    23     result[0] = 0;
    24     result[1] = 0;
    25     result[2] = log10((double)2);
    26     int i;
    27     for(i = 3; i < LEN; i++)
    28     {
    29         result[i] = result[i-1] + log10((double)i);
    30     }
    31     int cases;
    32     int n;
    33     scanf("%d", &cases);
    34     while (cases--)
    35     {
    36         scanf("%d", &n);
    37         if(n <= 800000)
    38             printf("%d\n",(int)result[n] + 1);
    39         else
    40             printf("%d\n", count(n));
    41     }    
    42     return 0;
    43 }
  • 相关阅读:
    SVN使用之分支、合并
    eq相等 ,ne、neq不相等 EL表达式
    PLSQL:plsql中文乱码,显示问号
    mysql中实现行号,oracle中的rowid
    安装完QQ必须要删除掉的几个恐怖文件
    关于table的一些兼容性问题
    Javascript事件绑定的几种方式
    jQuery中的一些正则匹配表达式
    将java类的泛型集合转换成json对象
    python登录豆瓣,发帖
  • 原文地址:https://www.cnblogs.com/null00/p/2560368.html
Copyright © 2011-2022 走看看