![](/img/trans.png)
[英]What is the greatest factorial we can calculate using an int in JAVA 19?
[英]Calculate factorial of 50 using array only in java
我是一个完全的java初学者。 我有一个作业来编写一个完整的程序,该程序使用数组计算 50 的阶乘。 我不能使用像 biginteger 这样的任何方法。 我只能使用数组,因为我的教授希望我们理解背后的逻辑,我猜......但是,他并没有真正教我们数组的细节,所以我在这里真的很困惑。
基本上,我试图划分大数并将其放入数组槽中。 所以如果第一个数组得到 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 < a.length; j++)
{
for(; n >= 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()),但我不确定这是正确的方法。 我的教授提到使用运算符 / 和 % 重复将数字分配给数组的下一个插槽。 所以我试图用它来做这个家庭作业。
有没有人有这个家庭作业的想法? 请帮我!
对令人困惑的指示感到抱歉......我也很困惑,所以请原谅我。
仅供参考: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;
}
}
怎么样:
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
怎么样:
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.