简体   繁体   English

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

[英]Calculate factorial of 50 using array only in java

I'm a total beginner of java.我是一个完全的java初学者。 I have a homework to write a complete program that calculates the factorial of 50 using array.我有一个作业来编写一个完整的程序,该程序使用数组计算 50 的阶乘。 I can't use any method like biginteger.我不能使用像 biginteger 这样的任何方法。 I can only use array because my professor wants us to understand the logic behind, I guess... However, he didn't really teach us the detail of array, so I'm really confused here.我只能使用数组,因为我的教授希望我们理解背后的逻辑,我猜......但是,他并没有真正教我们数组的细节,所以我在这里真的很困惑。

Basically, I'm trying to divide the big number and put it into array slot.基本上,我试图划分大数并将其放入数组槽中。 So if the first array gets 235, I can divide it and extract the number and put it into one array slot.所以如果第一个数组得到 235,我可以将它分割并提取数字并将其放入一个数组槽中。 Then, put the remain next array slot.然后,把剩下的下一个阵列槽。 And repeat the process until I get the result (which is factorial of 50, and it's a huge number..)重复这个过程,直到我得到结果(这是 50 的阶乘,这是一个巨大的数字..)

I tried to understand what's the logic behind, but I really can't figure it out.. So far I have this on my mind.我试图理解背后的逻辑是什么,但我真的无法弄清楚..到目前为止我一直在想这个。

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;
    }
}

But it doesn't show me the factorial of 50. it shows me 0 as the result, so apparently, it's not working.但它没有向我显示 50 的阶乘。它向我显示 0 作为结果,很明显,它不起作用。

I'm trying to use one method (fact()), but I'm not sure that's the right way to do.我正在尝试使用一种方法 (fact()),但我不确定这是正确的方法。 My professor mentioned about using operator / and % to assign the number to the next slot of array repeatedly.我的教授提到使用运算符 / 和 % 重复将数字分配给数组的下一个插槽。 So I'm trying to use that for this homework.所以我试图用它来做这个家庭作业。

Does anyone have an idea for this homework?有没有人有这个家庭作业的想法? Please help me!请帮我!

And sorry for the confusing instruction... I'm confused also, so please forgive me.对令人困惑的指示感到抱歉......我也很困惑,所以请原谅我。

FYI: factorial of 50 is 30414093201713378043612608166064768844377641568960512000000000000仅供参考:50 的阶乘是 30414093201713378043612608166064768844377641568960512000000000000

Try this.尝试这个。

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;
}

and

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

Her's my result:她是我的结果:

50 factorial - 30414093201713378043612608166064768844377641568960512000000000000

And here's the code.这是代码。 I hard coded an array of 100 digits.我硬编码了一个 100 位数字的数组。 When printing, I skip the leading zeroes.打印时,我跳过前导零。

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;
    }

}

how about:怎么样:

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;
}

Did not test this.没有测试这个。 No idea how big the number is and if it would cause error due to the size of the number.不知道这个数字有多大,以及它是否会因数字的大小而导致错误。

Updated.更新。 arrays use ".length" to measure the size.数组使用“.length”来衡量大小。

I now updated result to long data type and it returns the following - which is obviously incorrect.我现在将结果更新为长数据类型,它返回以下内容 - 这显然是不正确的。 This is a massive number and I'm not sure what your professor is trying to get at.这是一个庞大的数字,我不确定你的教授想要达到什么目的。 -3258495067890909184 -3258495067890909184

How about:怎么样:

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;

}

Please recall basic level of math how multiplication works?请回忆一下数学的基本水平,乘法是如何工作的?

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

So for m digits X n digits you need n list of string array.因此,对于 m 个数字 X n 个数字,您需要 n 个字符串数组列表。

Each time you multiply each digits with m.每次将每个数字与 m 相乘。 and store it.并存储它。

After each step you will append 0,1,2,n-1 trailing zero(s) to that string.在每一步之后,您将向该字符串附加 0,1,2,n-1 尾随零。

Finally, sum all of n listed string.最后,对所有列出的 n 个字符串求和。 You know how to do that.你知道怎么做。

So up to this you know m*n所以到目前为止你知道 m*n

now it is very easy to compute 1*..........*49*50.现在很容易计算 1*..........*49*50。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM