Input:
Enter 1 positive integers per line n, (0<n<1000 000)
Output:
For each n, the number of (decimal) digits of the output n!.
Analysis:
This problem uses brute force method. By definition, direct solution!
The so-called n! decimal digits, is log (n) +1, according to the mathematical formula is: n!=1*2*3*.....*n;
LG (n!) =LG (2) +......LG (n);
Code:
1234567891011121314151617181920212223242526272829303132 |
//输入一个数字n,请你计算该数的阶乘的十进制数的位数宽度
//比如:3!=6, 则宽度为1
//样例数据:
//n=3 输出1
//n=32000 输出130271
//n=1000000 输出5565709
#include <string>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cmath>
using
namespace
std;
int
main()
{
long
int
n;
long
int
i;
double
sum;
while
(
scanf
(
"%ld"
, &n)!=EOF)
{
sum=0.0;
for
(i=2; i<=n; i++)
{
sum+=
log10
(i);
}
printf
(
"%ld\n"
, (
int
)sum+1 );
}
return
0;
}
|
Category: Number theory, mind + logic + thinking
Calculates the number of digits of the factorial of a large number n (decimal) reproduced