HUD 1018

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! 的位数 = log10(n * (n - 1) * (n - 2) …) = log10(n) + log10(n - 1) + log10(n - 2) +…(不要忘了用double)

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int n; cin >> n;
    while(n --)
    {
        int a;
        cin >> a;
        double sum = 0;
        for(int i = 2; i <= a; i ++)   sum += log10(i);
        cout << (int)sum + 1 << endl; 
    }
    return 0;
}

可能我以后的我看到这个会笑出声来吧。哈哈~

#include <iostream>
#include <vector>

using namespace std;

const int N = 10000010;

int a[N];

vector<int> mul(vector<int> a, int b)
{
    int t = 0;
    
    vector<int> c;
    
    
    for(int i = 0; i < a.size(); i ++)
    {
        t += a[i] * b;
        
        c.push_back(t % 10);
        
        t /= 10;
        
    }
    while(t) c.push_back(t % 10), t /= 10;
    
    return c;
}

int main()
{
    vector<int> dig(1, 1);
    
    for(int i = 2; i <= 2000; i ++)
    {
        dig = mul(dig, i);
        
        a[i] = dig.size();
        //cout << a[i] << endl;
    }
    
    int n; scanf("%d" ,&n);
    
    while(n --)
    {
        int t; scanf("%d", &t);
        
        printf("%d\n", a[t]);
    }
    return 0;
}

发布了53 篇原创文章 · 获赞 14 · 访问量 1884

猜你喜欢

转载自blog.csdn.net/weixin_45630535/article/details/104908683
HUD