分分钟 面试题 n! 到底考什么?
来源:互联网 发布:js数组和对象的区别 编辑:程序博客网 时间:2024/04/29 08:25
关于n!每个程序员都能分分钟搞定
方法一:最简单的方法就是递推相乘:
代码如下,main中加入了很多输入参数的判断: 输入必须是一个 数字 :
package test.ms;public class TestJC { public static void main(String[] args) { try{ if(args == null || args.length >1 || args.length == 0){ System.out.println("Please input one number"); }else{ int n = Integer.parseInt(args[0]); factorial(n); } }catch(NumberFormatException e){ e.printStackTrace(); System.out.println("Please intput number formate "); } } public static void factorial(int n){ int result = 1; for(int i = n; i > 0; i--){ result = result*i; } System.out.println(result); }}以上就是递推相乘的代码,但是有一个严重的问题,就是int能够表示的范围:查看官方教程如下:
int: The int
data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long
instead.
int型最大表示正数是 2,147,483,647,当输入值为1-12的时候可以顺利计算出值,
12!为 479001600
13!就不对了。
如何才能写出通用的算出阶乘的方法:
通用的算出阶乘的方法要用到java中的BigInteger
BigInteger不可变的任意精度的整数。所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型)。BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。
利用BigInteger进行计算:
package test.ms;import java.math.BigInteger;public class TestJC { public static void main(String[] args) { try{ if(args == null || args.length >1 || args.length == 0){ System.out.println("Please input one number"); }else{ int n = Integer.parseInt(args[0]); System.out.println(n); factorial2(n); } }catch(NumberFormatException e){ e.printStackTrace(); System.out.println("Please intput number formate "); } } public static void factorial2(int n){ BigInteger result = new BigInteger("1"); for(int i=n ; i > 0 ; i--){ BigInteger temp = new BigInteger(String.valueOf(i)); result = result.multiply(temp); } System.out.println(result); }}
利用以上代码 100! =93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
能够准确计算出;
这种方法太简单,只是考察了 基本数据类型 int的取值范围:
方法二:递归实现
一般面试题中会要求进行递归实现,再进行考察递归:
递归很简单,一般程序员也是分分钟搞定:
普通方法代码如下:
package test.ms;public class TestFactorialRecursion { public static void main(String[] args) { try{ if(args == null || args.length >1 || args.length == 0){ System.out.println("Please input one number"); }else{ int n = Integer.parseInt(args[0]); int result = factorial(n); System.out.println(result); } }catch(NumberFormatException e){ e.printStackTrace(); System.out.println("Please intput number formate "); } } public static int factorial(int n){ if(n == 1){ return 1; }else{ return n*factorial(n-1); } }}以上代码是采用递归实现n!,但是由于 int 类型的 范围的局限性 ,所以 以上代码只能计算出 1--12的阶乘:
如果想计算出任意整数的阶乘,把以上代码替换为BigInteger形式即可:
全部采用BigInteger进行运算代码如下:
package test.ms;import java.math.BigInteger;public class TestFactorialRecursion { public static void main(String[] args) { try{ if(args == null || args.length >1 || args.length == 0){ System.out.println("Please input one number"); }else{ int n = Integer.parseInt(args[0]); BigInteger m = new BigInteger(String.valueOf(n)); BigInteger result = factorial2(m); System.out.println(result); } }catch(NumberFormatException e){ e.printStackTrace(); System.out.println("Please intput number formate "); } } public static BigInteger factorial2(BigInteger n){ BigInteger value1 = new BigInteger("1"); if( value1.compareTo(n) == 0){ return value1; }else{ BigInteger temp = new BigInteger(String.valueOf(n)); return temp.multiply(factorial2(temp.subtract(value1))); } }}
由于用到了递归,以上输入参数和返回类型都采用BigInteger类型:
BigInteger中提供了 关于 BigInteger 的加减乘除等一些基本运算:
上面的例子中:
用到了 multiply(BigInteger val) 和
subtract(BigInteger val)
方法 用于 乘和 减
输入
100!=93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
依然能够准确计算出来。
主要考察递归,基本类型值的范围。
- 分分钟 面试题 n! 到底考什么?
- 分分钟 面试题 n! 到底考什么?
- n年前传说中华为面试题(8分钟写出代码)
- 分分钟理解什么叫MVP模式
- Google 面试题:分饼干
- 只考加法的面试题
- 只考加法的面试题
- 只考加法的面试题
- 只考加法的面试题
- 2.21 只考加法的面试题
- 一道常考的javaSE面试题
- net常考的面试题
- 只考加法的面试题
- c/c++常考笔试面试题
- 只考加法的面试题
- 只考加法的面试题
- 只考加法的面试题
- 只考加法的面试题
- Ubuntu中各种软件安装方法。(不定期更新)
- The server encountered an internal error or misconfiguration and was ...
- CPU内存管理和linux内存分页机制
- java.lang.String字符串类
- 正确的在GridView中使用Bitmap图像
- 分分钟 面试题 n! 到底考什么?
- OSX,PHP,Mongo 安装配置的一些细节
- java经典面试
- java包的运行方式
- 利用URLrewrite实现伪静态地址输出
- (小白知识)ubuntu下安装.run文件
- 搜集汇总:计算机中乘除法的本质
- autorun.vbs
- 远程桌面开启“复制粘贴”功能