2016-03-05 413 views
2

我是java的初學者。 我有作業寫一個完整的程序,計算使用數組的50階乘。 我不能使用biginteger之類的任何方法。 我只能使用數組,因爲我的教授希望我們理解背後的邏輯,我猜... 但是,他並沒有真正教會我們數組的細節,所以我在這裏非常困惑。在java中使用數組計算50的階乘

基本上,我試圖劃分大數字,並將其放入數組插槽。因此,如果第一個數組獲得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的結果,所以很顯然,它不工作。

我想使用一種方法(事實()),但我不知道這是正確的方法。 我的教授提到有關使用operator /和%將數字重複分配給數組的下一個插槽。 所以我試圖用這個作業。

有沒有人有這個作業的想法? 請幫幫我!

對於令人困惑的指令感到抱歉...我也很困惑,所以請原諒我。

FYI:50階乘是30414093201713378043612608166064768844377641568960512000000000000

+1

提防[整數除法(http://stackoverflow.com/questions/7220681/division-of-integers-in-java) – Ian

+0

提示:你需要做乘法,當您使用筆就像和紙張。舉例來說,4! = 24,所以你的數組將是{2,4}。然後爲5!,你需要計算4 * 5,它給你0和2的進位,然後2 * 5 + 2給你2和進位1,所以你的新結果是{1,2, 0}。 –

回答

6

試試這個。

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 
+0

感謝您的幫助,但我不知道是否有另一種方式做到這一點,而不使用字符串。因爲我想通過整數主要方法來顯示數字,這是我的教授想... – learnerJ

+0

噢,我越來越近了!但是我仍然對後面的邏輯感到困惑。在int x = a [j] * i + carry中,執行程序時[j]的值是多少? – learnerJ

+0

謝謝!現在我明白了! – learnerJ

-1

怎麼樣:

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

+0

它不工作.. :(我將數組大小更改爲100,並且它也不工作 – learnerJ

+0

您不應該發佈您知道不正確的答案,這也不是OP遠程描述的方法 – Matthew

-1

如何:

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; 

} 
+2

問題說沒有'BigInteger'允許。 –

-1

請記得數學乘法是如何工作的基本水平?

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尾隨零(s)到該字符串。

最後,總結n列出的所有字符串。你知道該怎麼做。

所以到這一點,你知道m * n個

現在是很容易計算1 * .......... * 49 * 50。

0

她是我的結果:

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

}