Factorial Trailing Zeroes(OJ) 求其阶乘尾数0的个数[1808548329]
来源:互联网 发布:北京软件游戏编程培训 编辑:程序博客网 时间:2024/05/17 01:49
问题描述:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
问题分析:一个2和一个5相乘就会产生一个0,阶乘过程中5的个数肯定会比2多
例如: 5! =(2 * 2 * 2 * 3 * 5). 所以 0个数 is 1.
11! (2 8 * 34 * 52 * 7).所以 0个数 is 2.
问题转化为求阶乘过程中5的个数,而且注意25里有2个5,125有三个五,所以问题变为:
count= floor(n/5) + floor(n/25) + floor(n/125) + ....
网上有一个经典写法:
// Function to return trailing 0s in factorial of nint findTrailingZeros(int n){ // Initialize result int count = 0; // Keep dividing n by powers of 5 and update count for (int i=5; n/i>=1; i *= 5) count += n/i; return count;}在oj上提交会发现n = 1808548329时WA了
原因就是 i*5一直连乘时出现i = 14时,内存溢出(5^13 = 1220703125 < 2^31, but 5^14 = 6103515625 > 2^32)
但是 6103515625 % 2^32 = 1808548329 < 2 ^31,即1808548329 为合理输入
解决办法:
int trailingZeroes(int n) { int count = 0; for(int i = 5; n/i >= 1;) { count += n/i; n /= 5; } return count; }
2015年加油!注意细节,关注思想。
一直都断断续续,但是依然继续,我依然很菜,但是我依然坚持成长。
Trust in the Load with all your heart and learn not on your own understanding;
in all your ways acknowledge him, and he will make your paths straight. -[Proverbs 3:5-6]
- Factorial Trailing Zeroes(OJ) 求其阶乘尾数0的个数[1808548329]
- LeetCode 172. Factorial Trailing Zeroes(阶乘结果尾数0的个数)---E
- leetcode 172. Factorial Trailing Zeroes(C语言,阶乘中尾数0的个数)26
- 【LeetCode】Factorial Trailing Zeroes 阶乘尾部0的个数
- LeetCode:求阶乘后面的0的个数(Factorial Trailing Zeroes)
- Factorial Trailing Zeroes 阶乘结果中后缀为0的个数
- leetcode_172. Factorial Trailing Zeroes 阶乘中最末尾0的个数
- Factorial Trailing Zeroes 阶乘的后边有几个0
- Factorial Trailing Zeroes N!末尾0的个数
- 172. Factorial Trailing Zeroes计算n!末尾0的个数
- leetcode解题之172# Factorial Trailing Zeroes Java版 (求n的阶乘末尾0的个数)
- LeetCode OJ 之 Factorial Trailing Zeroes (阶乘尾部的零)
- leetcode Factorial Trailing Zeroes非负整数阶乘后尾0个数
- LeetCode OJ Factorial Trailing Zeroes
- LightOJ 1138 Trailing Zeroes (III) (阶乘末尾0的个数)
- 【Light-oj】-1138-Trailing Zeroes (III)(找到一个数使其阶乘后0的个数为n)
- LeetCode--Factorial Trailing Zeroes(阶乘的末位0数)
- 阶乘尾数0的个数
- Web_PHP_PHP-MySQL数据库浅谈;
- 以技术创新为核心的教育经济社区平台构建
- windows下redis安装
- mysql查询今天,昨天,近7天,近30天,本月,上一月数据方法分析总结
- Leetcode Binary Search Tree Iterator
- Factorial Trailing Zeroes(OJ) 求其阶乘尾数0的个数[1808548329]
- 使用Maven搭建Hadoop开发环境
- 什么是互联网技术
- 诺基亚、罗永浩,中国手机2014八大关键词
- 今天怎么回事啊
- 微软Windows Phone负责人自曝中文名
- 台湾批准联电7.1亿美元投资联芯科技
- 设计模式之结构型
- 展望2015:7大科技融入生活