当前位置:编程学习 > C/C++ >>

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

Problem

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

 

Solution


[cpp] 
#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; 

#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;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,