求一个数阶乘后位数问题

问题: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

回答:题意求一个数阶乘的位数.

方法一:

#include <stdio.h>
#include <math.h>
void main()
{
 int count,var,j;
 double sum;
 scanf("%d",&count);
 while(count--)
 {
  sum=0;
  scanf("%d",&var);
  for(j=1;j<=var;j++)
   sum+=log10(j);
  printf("%d/n",(int)sum+1);
 }
}
这是最简单的方法,但是耗时长,406ms。
好吧!我们换个方法:
这需要组合数学的知识。log10(n!) = log10(sqrt(2 * pi * n)) + n * log10(n / e)
 方法二:
#include <iostream>
#include <cmath>
#define PI 3.14159265
int n;
int num,ans;
void solve()
{
    double t;
    int i;
    t = (num*log(num) - num + 0.5*log(2*num*PI))/log(10);
    ans = t+1;
    printf("%d/n",ans);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        while(n--)
        {
            scanf("%d",&num);
            solve();
        }
    }
    return 0;
}

时间: 2024-08-19 09:44:44

求一个数阶乘后位数问题的相关文章

求一个数阶乘的位数

flyfish 2015-8-15 例如 7!=5040 ,7的阶乘结果是4位数(10进制) 求一个数的位数 1 循环方法 int get_digit_loop(int N) { int digit = 0; do { digit ++; } while ((N /= 10) > 0); return digit; } 2 递归方式 int get_digit_recursion(int N) { int digit = 0; digit = N < 10 ? 1 : 1 + get_digi

求一个数阶乘末尾0的个数

#include<iostream> using namespace std; //给定一个整数N,那么N的阶乘末尾有几个0?N=10,N!=3628800,末尾有2个0 //1.如果我们从"哪些数相乘能得到 10"这个角度来考虑,问题就变得简单了. //首先考虑,如果 N!= K×10M,且 K 不能被 10 整除,那么 N!末尾有 M 个 0.再考虑 //对 N!进行质因数分解,N!=(2x)×(3y)×(5z)-,由于 10 = 2×5,所以 M 只跟 X 和 Z /

POJ1423 Big Number 【求N的阶乘的位数】

Big Number Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26151   Accepted: 8349 Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encry

light_oj 1138 求阶乘后导零的个数

light_oj 1138  求阶乘后导零的个数 N - Trailing Zeroes (III) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1138 Description You task is to find minimal natural number N, so that N! contains exactly Q 

牛牛想对一个数做若干次变换,直到这个数只剩下一位数字。 变换的规则是:将这个数变成 所有位数上的数字的乘积。比如285经过一次变换后转化成2*8*5=80. 问题是,要做多少次变换,使得这个数变成个位数。

牛牛想对一个数做若干次变换,直到这个数只剩下一位数字.变换的规则是:将这个数变成 所有位数上的数字的乘积.比如285经过一次变换后转化成2*8*5=80.问题是,要做多少次变换,使得这个数变成个位数. 输入描述: 输入一个整数.小于等于2,000,000,000. 输出描述: 输出一个整数,表示变换次数. 输入例子: 285 输出例子: 2 1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 int n; 6 sc

10.输入一个数,求这个数的阶乘

(1)运用循环: #include<iostream>using namespace std;int JieCheng(int); int main(){    int n;    cout<<"please input an number: "<<endl;    cin>>n;    cout<<JieCheng(n);} int JieCheng(int n){    int m=1;    for(int i=1;i&

求n的阶乘以及前n个数的阶乘和

//求n的阶乘 #include<stdio.h> #include<stdlib.h> int main() { int n = 0; int sum = 1; scanf( "%d", &n); while (n)                             //n如果为零,则不进入循环体,输出的sum初始值就是1 { sum *=n; n--; } printf( "%d\n", sum); system( &quo

用递归函数求n的阶乘及斐波那契数列中的第n个数的值

题目要求:使用递归函数求n的阶乘及斐波那契数列中第n项的值. 1.求n的阶乘: 一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1. 计算公式为n!=1×2×3×...×n:递推公式可写作n!=n×(n-1)! 于是有: def fact(n): if n==1: return 1 return n*fact(n-1) n=int(input("请输入一个正整数:"))print(fact(n)) 2.求斐波那契数列中第n项的值: 形如1.1.2.

求n的阶乘的精确值

斯特林公式可以求出n!的近似值,但是如果需要求精确值的话,就要采取另外的办法了.' 当n<=1000的时候,可以直接模拟求阶乘,用一个数组保存阶乘的每一位,大概3000的数组就行.程序如下: #include<stdio.h> #include<string.h> #define N 3000 int f[N];//保存阶乘的位数 int main() { int n,i,j,c; while(~scanf("%d",&n)) { memset(f