2014-05-16 28 views
2

我的問題不是關於代碼,因爲它是編寫因子程序的邏輯。我目前正在赫爾辛基大學攻讀MOOC,並且我一直陷在這個練習中。隨着課程開始進行新的練習,說明變得越來越模糊。我意識到這可能不是問這個問題的地方,如果你必須標記或刪除它,我明白。我想自己學習,因爲我沒有時間或金錢去實際上大學。本課程沒有時間限制,我不會獲得成績證書,我只是想要知識。如何編碼因子

這些都爲鍛鍊

創建計算數n的階乘的程序給出的說明。因子n!使用公式1 * 2 * 3 * ... * n計算。例如4! = 1 * 2 * 3 * 4 = 24。另外,它被定義爲0! = 1

// i don't understand the example that 4!= 1*2*3*4 = 24 
    // or how 0! = 1 pertains to multiplying numbers in succession to find the 
    // factorial of the user input number. 
    // i understand that 0! = 1 simply delclares that 0 is not equal to 1 
    // and 4 is not equal to 24, however if the 4! = portion of this statement 
    // is in reference to the user input number 4 that statement would not be 
    // true as 1*2*3*4 does equal 24 and 4 would be the number of executions 
    // of the block execution of the loop required to write the factorial 
    // program. 

    // EDIT: okay so according to this http://en.wikipedia.org/wiki/Factorial 
    // i am wrong about what is being done here as they are not declaring 
    // that 4 not equals 24 but yet that 4! is a way of correlating the non 
    // negative numbers up to 4, but given that math is not my strong suit 
    // it is even more confusing to me as to what i should be doing. 

實施例輸出:

類型的數:3 階乘是6

類型的數:10 階乘是3628800

我當前的代碼的企圖是如下

 public static void main(String[] args) { 
    Scanner reader = new Scanner(System.in); 



    System.out.println("Type a number:"); 
    int userIn = Integer.parseInt(reader.nextLine()); 
    int factorial = 1; 
    int extra = 1; 
    int sum = 0; 

    while (factorial <= userIn) { 
     factorial++; 
     sum = factorial + userIn + extra; 
     } 
    System.out.println("The factorial is:"+sum); 
    } 
} 

我沒有看到我知道這是什麼,我錯過了,我從研究知道,在現實世界中,你不會編碼這個,因爲你可以下載庫來執行比我可以編碼更高效的因子函數,但是我不'我不想簡單地跳過這個練習,知道別人已經編碼並創建了一個圖書館來讓我們的生活更輕鬆,我想學習這門課程所提供的一切。如果我做了一個簡單的錯誤,我不介意提供的代碼更正,但是我想了解是什麼使得階乘操作可以這麼說,而不僅僅是給出答案,所以我可以繼續前進。

+2

我認爲你需要做的第一件事就是了解階乘運營商做什麼。一旦你明白了,代碼應該比較容易。維基百科的文章明確指出*真的*清楚。什麼因素混淆了你? – awksp

+4

'4!'的概念只是寫作'4 * 3 * 2 * 1'的一種緊湊方式。而已。任何整數的階乘本身都乘以小於它的每個整數。所以'5' ='5 * 4 * 3 * 2 * 1'同樣,'6!'='6 * 5!' – Kon

+1

定義:'n! = n *(n-1)!; 1! = 1!*(1-1)! => 1! == 0!'所以你應該考慮邊界約束=> factorial(0)== factorial(1)= 1。這就是訣竅;)。那麼如果(n> 1)?階乘(n-1)* n,否則返回1; –

回答

1

試試這一個,如果你不想使用外部功能

public static void main(String[] args) { 
    Scanner reader = new Scanner(System.in); 
    System.out.println("Type a number:"); 
    int userIn = Integer.parseInt(reader.nextLine()); 
    int factorial = 1; 
    int i= userin; 
    while (userin >= 1) { 
    factorial *= userIn; 
    userin--; 
    } 
    System.out.println("The factorial is:"+factorial); 
} 
} 
+0

這是我正在尋找,但我可以問爲什麼!沒有在解決方案中聲明? –

+0

你可以聲明一個!符號,而您正在打印靜態字符串,但不能用感嘆號進行操作,這隻能用於表示目的。 (我根據你的問題情況給出了這個評論,否則!在編程中是一個有用的操作符(我們主要使用這個作爲Not(位操作符))。 –

2

由n!表示的非負整數n的階乘是小於或等於n的所有正整數的乘積。例如: - 4!= 1 * 2 * 3 * 4。 0!= 1表示0的階乘是1而不是0不等於1。是1,根據空產品的慣例。空產品或空產品是不增加因素的結果。按照慣例等於乘法標識1,正如空總和(不加數字的結果)一樣,按慣例爲零(就像前0個自然數的總和就是0),即加性標識。 更多關於空產品讀到這裏http://en.wikipedia.org/wiki/Empty_product

對於編程部分,基本上有兩種方法來階乘程序: -

  1. 使用for循環(無遞歸)

    int factorial (int input) 
    { 
        int x, fact = 1; 
        for (x = input; x > 1; x--) // iterating from n -> n-1 -> n-2 ... 1 
         { 
         fact *= x;    // multiplying each number into the fact variable to get the factorial 
         } 
         return fact; 
        } 
    
  2. 遞歸方法 - 函數調用自己(請注意,避免在實際編程中使用此方法,因爲它可能會消耗大量資源並易於出錯,正如「Edwin Buck」在評論中指出的那樣)

    public int Factorial(int n) 
        { 
        if (n == 0) 
         { 
         return 1; //Base condition - If factorial reaches 0 return 1 and end recursion 
          } 
        else 
          { 
         return n * Factorial(n-1); // For factorial of n, function returns n * Factorial(n-1) i.e recursively calling the factorial function with one less value in the parameter untill 0 is reached (upon which base condtiion will be evaluated) 
          } 
        } 
    
+0

不要教遞歸階乘。當學習遞歸來呈現象階乘這樣的玩具問題時,它是有用的,但是在計算階乘時,這是一種可怕的資源浪費(和錯誤來源)。這類似於在分類時學習氣泡排序,不幸的是,像常用的氣泡排序示例一樣,人們大多記得他們首先提交的內容(無論其他選項多好)。 –

+0

感謝您指出,編輯了遞歸方法的描述。這是事實,用於計算階乘的遞歸方法(特別是對於大數量)是非常耗費資源的。如果需要編程一個階乘程序,我自己就不會使用這種方法。但只是給出了信息目的的方法。是的,我只是因爲它是在大學演示而重新遞歸的方法:D –

1

的問題是在這裏

sum = factorial + userIn + extra; 

你來自哪裏,在循環最新factorial++值 「計算」 的因子。

您無法以這種方式從總和中計算階乘。階乘都是1,且「階乘」數之間的整數的產品,所以

1! = 1 
    2! = 1 * 2 
    3! = 1 * 2 * 3 
    4! = 1 * 2 * 3 * 4 

如果你開始計算你的階乘錯了,那麼這個問題的另一部分沒有多大關係,他們將是錯誤通過擴展。

0
// Factorial example (ie 5 * 4 * 3 * 2 * 1) 
function factorial($n) { 

    if ($n == 1) return 1; 
    return $n * factorial($n-1); 

} 

echo factorial(5); // Outputs 120 
// Nested Array Summing Example 
$example = array(1, 2, array(10,20,30), 4); 

function sum_array($array) { 

    $total = 0; 
    foreach ($array as $element) { 
     if(is_array($element)) { 
      $total += sum_array($element); 
     } else { 
      $total += $element; 
     } 
    } 
    return $total; 

} 
echo sum_array($example); // Outputs 67