杭电 Big Number
来源:互联网 发布:mysql安装包下载 64位 编辑:程序博客网 时间:2024/04/28 03:43
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
21020
Sample Output
719
Source
Asia 2002, Dhaka (Bengal)
Recommend
JGShining | We have carefully selected several similar problems for you: 1017 1016 1071 1019 1060
题意:本题是要求阶乘的位数;
分析:此题看上去毫无头绪,但要是掌握了一个公式,就迎刃而解了;
x的位数就是(int)log10(x)+1;
推理如下:
对于任意一个给定的正整数a,
假设10^(x-1)<=a<10^x,那么显然a的位数为x位,
又因为
log10(10^(x-1))<=log10(a)<(log10(10^x))
即x-1<=log10(a)<x
则(int)log10(a)=x-1,
即(int)log10(a)+1=x
即a的位数是(int)log10(a)+1
假设10^(x-1)<=a<10^x,那么显然a的位数为x位,
又因为
log10(10^(x-1))<=log10(a)<(log10(10^x))
即x-1<=log10(a)<x
则(int)log10(a)=x-1,
即(int)log10(a)+1=x
即a的位数是(int)log10(a)+1
于是阶乘的位数便出来了,由于阶乘数比较大,故根据数学性质可写成循环实现;
代码如下:
int main(){int n,a,i,j;double s;scanf("%d",&n);for(i=0;i<n;i++){s=0;scanf("%d",&a);for(j=1;j<=a;j++)s+=log10(j); //公式:x的位数就是(int)log10(x)+1; printf("%d\n",(int)s+1); //故x阶乘的位数就是(int)log10(x!)+1; } //因阶乘的位数较大,故x!用for循环来实现 return 0;}
0 0
- 杭电 Big Number
- 杭电1018 Big Number
- 杭电1018 Big Number
- 杭电 1018 Big Number
- 杭电1018-Big Number
- 杭电 1018 Big Number
- 杭电 1018 Big Number
- 杭电 1212 Big Number
- 杭电1018 Big number
- 杭电1212 Big Number
- 杭电1018 Big Number
- 杭电1212Big Number
- 杭电1018Big Number
- 【杭电】[1212]Big Number
- 杭电1018Big Number
- 杭电1018 BIG NUMBER
- 杭电OJ1212-Big Number
- 杭电--1018 Big Number
- C语言总结2
- poj3616 Milking Time
- 关于VS2008问题“无法找到“XXX.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件。“
- LeetCode-Maximum Subarray
- au3宏指令参考 - 完整宏指令列表
- 杭电 Big Number
- Sorting a 2d array in ruby
- hibernate的映射:一对多关联关系
- 选择适合自己的流产方式
- CUDA常量内存
- Android导入一个工程时提示 Invalid project description的解决方法
- linux检测网线插拔状态
- Qt中动态链接库的使用(QPluginLoader方法)
- Android 多线程-----AsyncTask详解