求阶乘的尾0,要求对数时间复杂度。
解决:
只有2*5能得出0,对于阶乘,各项因子,因子2的数量肯定大于5,求5即可。
含有5的因子有5,25...
int trailingZeroes(int n) { int ret = 0; long now = 5; while (now <= n) { ret += n / now; now *= 5; } return ret; }
本文共 301 字,大约阅读时间需要 1 分钟。
求阶乘的尾0,要求对数时间复杂度。
解决:
只有2*5能得出0,对于阶乘,各项因子,因子2的数量肯定大于5,求5即可。
含有5的因子有5,25...
int trailingZeroes(int n) { int ret = 0; long now = 5; while (now <= n) { ret += n / now; now *= 5; } return ret; }
转载于:https://www.cnblogs.com/willaty/p/8379724.html