hdu 1018 共同交流~

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38886    Accepted Submission(s):
18889

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!的十进制位数,就是 log(n)+1, 根据数学公式有:n!=1*2*3*.....*n;
//lg(n!)=lg(2)+......lg(n);
//则位数 = log(n)+1;

代码如下:

#include<stdio.h>
#include<math.h>
double f(int n)
{
  double cnt=0;
  for(double i=2;i<=n;i++) // i也要为double型,否则会出现歧义 !
  {
    cnt += log10(i);
  }
  return cnt;
}

int main()
{
  int cas,n;
  scanf("%d",&cas);
  while(cas--)
  {
    scanf("%d",&n);
    printf("%d\n",(int)f(n) + 1);
  }
  return 0;
}

时间: 2024-10-12 23:43:28

hdu 1018 共同交流~的相关文章

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

HDU 1018 Big Number (简单数学)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25649    Accepted Submission(s): 11635 Problem Description In many applications very large integers numbers are required. Some of these

HDU 1018 Big Number (数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x) + 1; 所以n!的位数 = (int)log10(1*2*3*.......n) + 1; = (int)(log10(1) + log10(2) + log10(3) + ........ log10(n)) + 1; 1 #include<cstdio> 2 #include<cs

HDU 1018(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27548    Accepted Submission(s): 12526 Problem Description In many applications ve

HDU 1018 -- Big Number (Stirling公式求n!的位数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 40551    Accepted Submission(s): 19817 Problem Description In many applications ver

HDU 1018 Big Number

有个数学公式计算数的阶乘位数 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include<cmath> 5 #define pi 3.141592653 6 #define E 2.718281828 7 using namespace std; 8 9 int main() 10 { 11 12 freopen("C:\\Users\\super\\Docum

HDU 1018 大数(求N!的位数/相加)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35382    Accepted Submission(s): 16888 Problem Description In many applications very large integers numbers are required. Some of these

HDU 1018

题意:求n!的位数 题解:斯特林数: log10(n!)=1.0/2*log10(2*pi*n)+n*log10(n/e). 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #define E 2.71828182 6 #define PI acos( -1.0 ) 7 8 int n; 9 10 int main() 11 { 12

HDU 1018 Big Number (log函数求数的位数)

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