簡體   English   中英

僅在java中使用數組計算50的階乘

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM