hdu 1124 Factorial (快速求n!末尾0的个数)

思路:f(n)表示n!的末尾0的个数,则

f(n) = n/5 + f(n/5)  n >= 5

f(n) = 0; n < 5


证明:点击


代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define eps 10e-8

const int MAX_ = 10010;
const int N = 100010;
const int INF = 0x7fffffff;

int a[MAX_];

int find(int n){
    if(n < 5)return 0;
    return n/5 + find(n/5);
}

int main(){
	int n, T;
	scanf("%d", &T);
	while(T--){
        scanf("%d", &n);
        int ans = find(n);
        printf("%d\n", ans);
    }
	return 0;
}


你可能感兴趣的:(hdu 1124 Factorial (快速求n!末尾0的个数))