LintCode #2 尾部的零
计算阶乘尾部的0的个数,初一看很简单。
先上代码
public static long GetFactorial(long n)
{
if (n == || n == )
return ; return n*GetFactorial(n - );
} //Main方法中调用 var result = GetFactorial(); int num = ;
int count = ;
while (true)
{
if (result % num == )
{
num = num * ;
count++;
}
else
{
break;
}
}
//count就是最终的结果
提交以后才发现问题,计算阶乘的数太大,会导致溢出。查了会资料,用数组存储数字,就不会有溢出的问题了。比如数字120, 存在数组里的结果是 a[0]=0, a[1]=2, a[2]=1
public static List<int> GetFactorial(int n)
{
List<int> r = new List<int>()
{ }; if (n == || n == )
return r; int num = ; for (int j = ; j <= n; j++)
{
for (int i = ; i < r.Count; i++)
{
var tmp = r[i] * j + num;
if (tmp > )
{
r[i] = tmp % ;
num = tmp / ;
}
else
{
r[i] = tmp;
num = ;
}
} if (num > )
{
r.Add(num);
num = ;
}
} return r;
} //Main方法中调用, count就是最终的结果
var list = GetFactorial(); int count = ;
for (int i = ; i < list.Count; i++)
{
if (list[i] == )
{
count++;
}
else
{
break;
}
}
计算105或者在大一点的数据的阶乘没有问题,不过在提交的时候有又情况了。
LintCode给出的测试数据是1001171717的阶乘,期望结果是250292920。提示错误为:Memory Limit Exceeded。一定是在用数组存储阶乘结果的时候,内存超限了。
运行了一下,想看看这个数的阶乘是多少,结果运行了好长时间还没有算完,还需要进行优化,考虑中...
LintCode #2 尾部的零的更多相关文章
- lintcode :Trailing Zeros 尾部的零
题目: 尾部的零 设计一个算法,计算出n阶乘中尾部零的个数 样例 11! = 39916800,因此应该返回 2 挑战 O(logN)的时间复杂度 解题: 常用方法: 也许你在编程之美中看到,通过求能 ...
- LintCode——尾部的零
尾部的零:设计一个算法,计算出n阶乘中尾部零的个数 样例:11! = 39916800.因此应该返回2 分析:假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,例如 1 × 2 × 3 ...
- [LintCode] 尾部的零
class Solution { public: // param n : description of n // return: description of return long long tr ...
- [LintCode] Trailing Zeroes 末尾零的个数
Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...
- [LintCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- #4018. 统计n! 尾部零
题目出处: http://www.51cpc.com/problem/4018 题目描述 试统计正整数n的阶乘n!=1×2×3×…×n尾部连续零的个数. 输入格式 输入正整数n 输出格式 输出个数 样 ...
- Lintcode答案&笔记
1.A+B问题 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符 思路:作异或得到未进位和,作与并向左移1位得到进位,随后再重复操作俩结果,直到进位为0,适合用递归 public in ...
- C++程序员面试题目总结(涉及C++基础、多线程多进程、网络编程、数据结构与算法)
说明:C++程序员面试题目总结(涉及C++基础知识.多线程多进程.TCP/IP网络编程.Linux操作.数据结构与算法) 内容来自作者看过的帖子或者看过的文章,个人整理自互联网,如有侵权,请联系作者 ...
- C Primer Plus(第五版)10
第 10 章 数组和指针 在本章中你将学习下列内容: · 关键字: static (静态) · 运算符: & * (一元) · 创建与初始化数组的方法. · 指针(基于已学的基础知识)及指针和 ...
随机推荐
- VS2015终极卸载方法
今天打开VS2015发现出问题了,总是停止响应,去控制面板里卸载结果像下面这样,卸载出错!于是我有开始折腾了,重新安装一遍然后,还是有问题,在卸载还是出错于是我决定通过安装介质卸载,结果,悲剧的是,启 ...
- python将列表元素按指定数目分组
比如,有时候,我们需要将列表中的元素,按照每5个分组,分成好几个组时,可以采用下面的代码 a = [1,2,3,4,5,6,7,8,9,10,11] step = 5 b = [a[i:i+step] ...
- spark on alluxio和MR on alluxio测试(改进版)【转】
转自:http://kaimingwan.com/post/alluxio/spark-on-alluxiohe-mr-on-alluxioce-shi-gai-jin-ban 1. 介绍 2. 准备 ...
- 部署openfire到linux环境下
1.java环境部署:具体参考 http://blog.csdn.net/gufachongyang02/article/details/45337379 2.ant环境部署: 具体参考 http:/ ...
- MyBatis教程目录
MyBatis教程目录 2017-10-18 摘自 YSOcean MyBatis教程目录: 1 mybatis 详解(一)------JDBC 2 mybatis 详解(二)------入门实例( ...
- 显示eclipse中Problem窗口的方法
https://blog.csdn.net/ningfuxuan/article/details/76395029 ****************************************** ...
- error LNK2005: _DllMain@12 已经在 MSVCRTD.lib(dllmain.obj) 中定义
备注:我上次遇到这个问题是Win32 DLL项目中无意中include了afxwin.h,这个是MFC的头文件,把这个include删掉就解决了 ================ 转自:http:// ...
- LR中,URL -based script与HTML -based script区别
在Web(HTTP/HTML)录制中,有2种重要的录制模式.用户该选择那种录制模式呢?HTML-mode录制是缺省也是推荐的录制模式.它录制当前网页中的HTML动作.在录制会话过程中不会录制所有的资源 ...
- [note]Why I haven’t quit my corporate job (yet)
Why I haven't quit my corporate job (yet)html, body {overflow-x: initial !important;}html { font-siz ...
- 腾讯云安装Hadoop遇到的问题
1.能正常访问bincoding.cn:50030,不能连接到9000端口 Call to bincoding.cn/123.206.212.115:9000 failed on connection ...