思路:有兩種方法能夠解決:java
法一:(真的非常暴力)使用大數存儲類BigInteger,只要你電腦內存夠你就能夠輸出夠大的數web
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 public class Main{ 5 6 public static void main(String[] args) { 7 Scanner in=new Scanner(System.in); 8 Factor F=new Factor(); 9 int N; 10 N=in.nextInt(); 11 BigInteger fac; 12 fac=F.factor(N); 13 System.out.println(fac); 14 } 15 } 16 17 class Factor{ 18 BigInteger factor(int N) { 19 if(N==1||N==0) { 20 return BigInteger.valueOf(1); 21 }else { 22 return BigInteger.valueOf(N).multiply(factor(N-1)); 23 } 24 } 25 }
法二:較以前的法一比較,較爲技巧一點。咱們使用數組來進行大數的存儲。代碼不長在這裏再也不進行多餘的解釋。算法
1 import java.util.Scanner; 2 3 public class Main{ 4 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 int[] array = new int[100000]; 8 int N; 9 array[0] = 1; 10 N = in.nextInt(); 11 for (int i = 1; i <= N; i++) { 12 int r = 0; 13 for (int j = 0; j < array.length; j++) { 14 int temp = array[j] * i + r; 15 array[j] = temp % 10; 16 r = temp / 10; 17 } 18 } 19 20 boolean begin = false; 21 22 for (int i = array.length - 1; i >= 0; i--) { 23 if (begin) { 24 System.out.print(array[i]); 25 continue; 26 } 27 if (array[i - 1] != 0) { 28 begin = true; 29 } 30 } 31 } 32 }