现在的位置: 首页 > 综合 > 正文

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

2013年08月08日 ⁄ 综合 ⁄ 共 3601字 ⁄ 字号 评论关闭

关于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

依然能够准确计算出来。

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



抱歉!评论已关闭.