zoukankan      html  css  js  c++  java
  • hdu 1018 Big Number

    Problem 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 ≤ n ≤ 107 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
    n!的位数len=log10(n!)=log10(n)+log10(n-1)+log10(n-2)+......+log10(2)+log10(1)+1.再对len取整就可以了
    直接求代码如下:差点超时
    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int n,t,i;
        double count;
        scanf("%d",&t);
        while(t--)
        {
            count=0;scanf("%d",&n);
            for(i=2;i<=n;i++)
                count+=log10(i);
            printf("%d
    ",int(count)+1);
        }
        return 0;
    }

    下面介绍一个一个公式log10(n!)=1.0/2*log10(2*pi*n)+n*log10(n/e)

    lnN!=NlnNN+0.5ln(2N*pi) 

    N的阶乘的位数等于log10(N!)取整后加1

    log10(N!)=lnN!/ln(10)所以len=lnN!/ln(10)+1

    #include<stdio.h>
    #include<math.h>
    const double pi=acos(-1.0);
    int main()
    {
        int t,n,count;
        scanf("%d",&t);
        while(t--)
        {
            count=0;
            scanf("%d",&n);
            count=(0.5*log(2*pi*n)+n*log(n)-n)/log(10);
            printf("%d
    ",count+1);
        }
        return 0;
    }

     

     

  • 相关阅读:
    第二部分 设计类型:第8章 方法
    centos7 网卡重命名
    centos7修改主机名
    修改umask值
    mysql表字段属性
    mysql基础操作
    mysql错误代码ERROR 1045 (转载)
    sed高级用法
    shell拷贝原文件到目标对应文件夹
    函数(位置传参)
  • 原文地址:https://www.cnblogs.com/duan-to-success/p/3506836.html
Copyright © 2011-2022 走看看