对任意输入的正整数N,编写C程序求N!的尾部连续0的个数...

 

 对任意输入的正整数N,编写C程序求N!的尾部连续0的个数,并指出计算复杂度。如:18!=6402373705728000,尾部连续0的个数   是3
(不用考虑数值超出计算机整数界限的问题)


#include <cstdlib> #include <iostream> using namespace std; int getCount(int n) { int count = 0; for(int i=5; i<=n; i += 5) { int j = i; while(j % 5 == 0) { count++; j /= 5; } } return count; } int main(int argc, char *argv[]) { int n; cin >> n; cout << getCount(n) << endl; system("PAUSE"); return EXIT_SUCCESS; } 

 

当一个数字中含有因子5时, 结果肯定有一个0, 于是, 求N的阶乘中所有数因子5的个数, 就是最后的0的个数...

 

你可能感兴趣的:(c,System)