计算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;

}


 

 

你可能感兴趣的:(计算)