阶乘尾后0的数目
2016-12-27 00:00
218 查看
原题
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.
题目大意
给定一个整数n,求阶乘结果的尾部0的个数。解题思路
假如i可以被5整除,则可以提供的5的个数为i/5个假如i可以被25整除,则可以多提供的5的个数为i/25个
假如i可以被125整除,则可以多提供的5的个数为i/125个(算上了被5,25整除之后)
。。。
代码实现
算法实现类public class Solution { // 假如i可以被5整除,则可以提供的5的个数为i/5个 public int trailingZeroes(int n) { int result = 0; long tmp = n; // 为了防止i*5超出int的表大表示范围 for (long i = 5; i <= tmp; i *= 5) { result += n / i; } return result; } }