PKU 1401 Factorial 有一個計算公式

題目鏈接:http://poj.org/problem?id=1401

題意:求N!的末尾0的個數。

思路:這算是一個數學公式求解型的問題。方法是,給定一個數N,結果就是: [N/5]+[N/25]+[N/125]+............+[N/(5^k)],其中5^k爲不大於N的最大的數。具體原理自行百度即可。

代碼:

#include<iostream>

using namespace std;

int N;

int main(){
    int T;cin>>T;
    while(T--){
        cin>>N;
        int t=5,ans=0;
        while(t<=N){
            ans+=(N/t);
            t*=5;//動態處理
        }
        cout<<ans<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章