计算N阶乘中结尾有多少零

Problem

Write an algorithm which computes the number of trailing zeros in n factorial.

Solution

 

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    int n = 100;
    int m = n;

    int num;

    num = 0;

    while( n / 5 > 0){
        num += n / 5;
        n /= 5;
    }

    cout << "factoral ("  << m << ")" << " has " << num << " zero trailings. " << endl;

    return 0;
}


 

 

posted @ 2013-05-17 22:10  javawebsoa  Views(209)  Comments(0Edit  收藏  举报