Statistics n! Number of last 0

To count the number of 0, you only need to count the number of 2*5. However, in this series, the number of 5 is far less than the number of 2, so you only need to count the number of 5.

 

 

CODE:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;

#define LL long long
const int N=1e6+10;

int main()
{
    int n;
    while(cin>>n)
    {
        int tmp=0;
        int ans=1;
        while(n)
        {
            tmp+=(n/5);    //统计5的个数
            n/=5; // 其实不断除以 5, 是因为每间隔 5 个数有一个数可以被 5 整除, 然后在这些可被 5 整除的数中, 每间隔 5 个数又有一个可以被 25 整除, 故要再除一次, ... 直到结果为 0, 表示没有能继续被 5 整除的数了.
        }
        cout<<tmp<<endl;
    }
    return 0;
}
 In fact, we keep dividing by 5 because every interval of 5 numbers has a number that is divisible by 5, and among these numbers that are divisible by 5, there is another number in every interval that is divisible by 25, so it needs to be divided again. , ... until the result is 0, which means that there is no number that can continue to be divisible by 5.
        }
        cout<<tmp<<endl;
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/Puppet__/article/details/80501186