n阶乘 尾数0的个数
2015-07-16 21:11
211 查看
class Solution {
public:
int trailingZeroes(int n) {
if(n<=0) return 0;
int i=0;
int res=0;
while(n){
res+=n/5;
n=n/5;
}
return res;
}
};
很神奇的,eg 125
125=25*5,相当于前前面有1,2,3,4,5,……,20,21,22,23,24,25 * 5
125/5 = 25 相当于前面变成0,0,0,0,1,……,4,4,4,4,4,5 *5
25/5=5 所以最后结果结果过就是25+5+1
public:
int trailingZeroes(int n) {
if(n<=0) return 0;
int i=0;
int res=0;
while(n){
res+=n/5;
n=n/5;
}
return res;
}
};
很神奇的,eg 125
125=25*5,相当于前前面有1,2,3,4,5,……,20,21,22,23,24,25 * 5
125/5 = 25 相当于前面变成0,0,0,0,1,……,4,4,4,4,4,5 *5
25/5=5 所以最后结果结果过就是25+5+1
相关文章推荐
- 设计模式之命令模式
- 例题3-1 TeX中的引号(TeX)
- 离线搭建 Android 集成开发环境
- chapter18test3
- REST HTTP中的POST PUT GET
- poj 1945 Power Hungry Cows(BFS)(简单题)
- 人丑就要多读书(11)
- Windows7下MySQL5.5.20免安装版的配置
- Codeforces#277 C,E
- [翻译] UIColor-uiGradientsAdditions
- The Services(服务)
- git 常用命令
- Cocos2d-JS开发环境搭建
- 最快Android模拟器Genymotion的安装与使用完整教程(多图,慎点)
- ListView/GridView 加载图片性能优化技巧
- 文字信息无缝滚动效果
- Java中判断字符串是否为数字的五种方法
- C++ 中 char 与 int 转换问题
- scikit-learn:4. 数据集预处理(clean数据、reduce降维、expand增维、generate特征提取)
- 匹配问题解决