仅在java中使用数组计算50的阶乘

我是java的初学者。 我有一个功课来编写一个完整的程序,使用数组计算50的阶乘。 我不能使用像biginteger这样的方法。 我只能使用数组,因为我的教授希望我们理解背后的逻辑,我猜……但是,他并没有真正教我们数组的细节,所以我真的很困惑。

基本上,我试图划分大数字并将其放入arrays插槽。 因此,如果第一个数组得到235,我可以将其除去并提取数字并将其放入一个数组槽中。 然后,将剩余的下一个数组插槽。 并重复这个过程,直到我得到结果(这是50的阶乘,这是一个巨大的数字..)

我试图理解背后的逻辑是什么,但我真的无法理解……到目前为止,我有这个想法。

import java.util.Scanner; class Factorial { public static void main(String[] args) { int n; Scanner kb = new Scanner(System.in); System.out.println("Enter n"); n = kb.nextInt(); System.out.println(n +"! = " + fact(n)); } public static int fact(int n) { int product = 1; int[] a = new int[100]; a[0] = 1; for (int j = 2; j = 1; n--) { product = product * n; a[j-1] = n; a[j] = a[j]/10; a[j+1] = a[j]%10; } } return product; } } 

但它并没有向我展示50的阶乘。它显示了0作为结果,显然,它不起作用。

我正在尝试使用一种方法(fact()),但我不确定这是正确的方法。 我的教授提到过使用operator /和%将数字重复分配给数组的下一个插槽。 所以我正在尝试将它用于这项功课。

有没有人有这个家庭作业的想法? 请帮我!

抱歉这令人困惑的指示……我也很困惑,所以请原谅我。

仅供参考:50的阶乘是30414093201713378043612608166064768844377641568960512000000000000

尝试这个。

 static int[] fact(int n) { int[] r = new int[100]; r[0] = 1; for (int i = 1; i <= n; ++i) { int carry = 0; for (int j = 0; j < r.length; ++j) { int x = r[j] * i + carry; r[j] = x % 10; carry = x / 10; } } return r; } 

 int[] result = fact(50); int i = result.length - 1; while (i > 0 && result[i] == 0) --i; while (i >= 0) System.out.print(result[i--]); System.out.println(); // -> 30414093201713378043612608166064768844377641568960512000000000000 

她是我的结果:

 50 factorial - 30414093201713378043612608166064768844377641568960512000000000000 

这是代码。 我硬编码了一个100位的数组。 打印时,我跳过前导零。

 public class FactorialArray { public static void main(String[] args) { int n = 50; System.out.print(n + " factorial - "); int[] result = factorial(n); boolean firstDigit = false; for (int digit : result) { if (digit > 0) { firstDigit = true; } if (firstDigit) { System.out.print(digit); } } System.out.println(); } private static int[] factorial(int n) { int[] r = new int[100]; r[r.length - 1] = 1; for (int i = 1; i <= n; i++) { int carry = 0; for (int j = r.length - 1; j >= 0; j--) { int x = r[j] * i + carry; r[j] = x % 10; carry = x / 10; } } return r; } } 

怎么样:

 public static BigInteger p(int numOfAllPerson) { if (numOfAllPerson < 0) { throw new IllegalArgumentException(); } if (numOfAllPerson == 0) { return BigInteger.ONE; } BigInteger retBigInt = BigInteger.ONE; for (; numOfAllPerson > 0; numOfAllPerson--) { retBigInt = retBigInt.multiply(BigInteger.valueOf(numOfAllPerson)); } return retBigInt; } 

请回想一下基本的数学运算水平如何运算?

 2344 X 34 = (2344*4)*10^0 + (2344*3)*10^1 = ans 2344 X334 = (2344*4)*10^0 + (2344*3)*10^1 + (2344*3)*10^2= ans 

因此,对于m位X n位数,您需要n个字符串数组列表。

每次将每个数字乘以m。 并存储它。

在每个步骤之后,您将向该字符串附加0,1,2,n-1尾随零。

最后,总结所有n个列出的字符串。 你知道怎么做。

所以你知道m * n

现在很容易计算1 * ………. * 49 * 50。

怎么样:

 int[] arrayOfFifty = new int[50]; //populate the array with 1 to 50 for(int i = 1; i < 51; i++){ arrayOfFifty[i-1] = i; } //perform the factorial long result = 1; for(int i = 0; i < arrayOfFifty.length; i++){ result = arrayOfFifty[i] * result; } 

没试过这个。 不知道数字有多大,以及是否由于数字的大小而导致错误。

更新。 数组使用“.length”来测量大小。

我现在将结果更新为长数据类型并返回以下内容 - 这显然是不正确的。 这是一个庞大的数字,我不确定你的教授想要得到什么。 -3258495067890909184