分分钟 面试题 n! 到底考什么?

来源:互联网 发布:苹果网络锁破解 编辑:程序博客网 时间:2024/04/30 10:20

关于n!每个程序员都能分分钟搞定

方法一:最简单的方法就是递推相乘:

代码如下,main中加入了很多输入参数的判断: 输入必须是一个 数字 :

[java] view plain copy
  1. package test.ms;  
  2.   
  3.   
  4. public class TestJC {  
  5.    public static void main(String[] args) {  
  6.        
  7.       try{  
  8.           if(args == null || args.length >1 || args.length == 0){  
  9.              System.out.println("Please input  one  number");  
  10.           }else{  
  11.               int  n = Integer.parseInt(args[0]);  
  12.               factorial(n);  
  13.           }  
  14.             
  15.       }catch(NumberFormatException e){  
  16.           e.printStackTrace();  
  17.           System.out.println("Please  intput  number  formate ");  
  18.       }  
  19.         
  20. }  
  21.    public  static  void  factorial(int  n){  
  22.        int   result = 1;  
  23.           for(int i = n; i > 0; i--){  
  24.               result = result*i;  
  25.           }  
  26.       System.out.println(result);  
  27.    }  
  28. }  
以上就是递推相乘的代码,但是有一个严重的问题,就是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进行计算:

[java] view plain copy
  1. package test.ms;  
  2.   
  3. import java.math.BigInteger;  
  4.   
  5. public class TestJC {  
  6.    public static void main(String[] args) {  
  7.        
  8.       try{  
  9.           if(args == null || args.length >1 || args.length == 0){  
  10.              System.out.println("Please input  one  number");  
  11.           }else{  
  12.               int  n = Integer.parseInt(args[0]);  
  13.               System.out.println(n);  
  14.               factorial2(n);  
  15.           }  
  16.             
  17.       }catch(NumberFormatException e){  
  18.           e.printStackTrace();  
  19.           System.out.println("Please  intput  number  formate ");  
  20.       }  
  21.         
  22. }  
  23.     
  24.      
  25.      
  26.    public  static  void  factorial2(int  n){  
  27.        BigInteger  result = new  BigInteger("1");  
  28.        for(int i=n ; i > 0 ; i--){  
  29.            BigInteger  temp = new  BigInteger(String.valueOf(i));  
  30.             result = result.multiply(temp);  
  31.        }  
  32.        System.out.println(result);  
  33.          
  34.    }  
  35. }  

利用以上代码 100! =93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

能够准确计算出;


这种方法太简单,只是考察了 基本数据类型 int的取值范围:

方法二:递归实现

一般面试题中会要求进行递归实现,再进行考察递归:

递归很简单,一般程序员也是分分钟搞定:

普通方法代码如下:

[java] view plain copy
  1. package test.ms;  
  2.   
  3. public class TestFactorialRecursion {  
  4.   public static void main(String[] args) {  
  5.            
  6.       try{  
  7.           if(args == null || args.length >1 || args.length == 0){  
  8.              System.out.println("Please input  one  number");  
  9.           }else{  
  10.               int  n = Integer.parseInt(args[0]);  
  11.              int  result = factorial(n);  
  12.              System.out.println(result);  
  13.           }  
  14.             
  15.       }catch(NumberFormatException e){  
  16.           e.printStackTrace();  
  17.           System.out.println("Please  intput  number  formate ");  
  18.       }  
  19.   }  
  20.         
  21.       public  static  int   factorial(int  n){  
  22.           if(n == 1){  
  23.               return  1;  
  24.           }else{  
  25.               return   n*factorial(n-1);  
  26.           }  
  27.       }  
  28. }  
以上代码是采用递归实现n!,但是由于 int 类型的 范围的局限性 ,所以 以上代码只能计算出 1--12的阶乘:

如果想计算出任意整数的阶乘,把以上代码替换为BigInteger形式即可:

全部采用BigInteger进行运算代码如下:

[java] view plain copy
  1. package test.ms;  
  2.   
  3. import java.math.BigInteger;  
  4.   
  5. public class TestFactorialRecursion {  
  6.   public static void main(String[] args) {  
  7.            
  8.       try{  
  9.           if(args == null || args.length >1 || args.length == 0){  
  10.              System.out.println("Please input  one  number");  
  11.           }else{  
  12.               int  n = Integer.parseInt(args[0]);  
  13.               BigInteger   m = new BigInteger(String.valueOf(n));  
  14.              BigInteger  result = factorial2(m);  
  15.              System.out.println(result);  
  16.           }  
  17.             
  18.       }catch(NumberFormatException e){  
  19.           e.printStackTrace();  
  20.           System.out.println("Please  intput  number  formate ");  
  21.       }  
  22.   }  
  23.       public  static  BigInteger  factorial2(BigInteger  n){  
  24.           BigInteger   value1 = new  BigInteger("1");  
  25.           if( value1.compareTo(n) == 0){  
  26.               return   value1;  
  27.           }else{  
  28.               BigInteger  temp = new  BigInteger(String.valueOf(n));  
  29.               return   temp.multiply(factorial2(temp.subtract(value1)));  
  30.           }  
  31.             
  32.       }  
  33. }  

由于用到了递归,以上输入参数和返回类型都采用BigInteger类型:

BigInteger中提供了 关于 BigInteger 的加减乘除等一些基本运算:

上面的例子中:

用到了 multiply(BigInteger val) 和subtract(BigInteger val)  方法 用于 乘和 减

输入 

100!=93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

依然能够准确计算出来。

主要考察递归,基本类型值的范围。

0 0
原创粉丝点击