2016-04-25 28 views
0

我必須使用遞歸找到不能被7整除的所有偶數的和。我想這個代碼,但它似乎我犯錯誤的地方,因爲它返回0:Java遞歸不可被特定數字整除的數字

public static void main(String[] args) { 

System.out.println(specialSum(50)); 
} 
public static int specialSum(int a) { 

    if ((a >= 1) && ((specialSum(a-1))%7 !=0)) { 
     return a + specialSum(a -1); 
    } else{ 
     return 0; 
    } 

    } 
} 
+1

那麼,遞歸意味着你繼續迭代,直到你達到最終狀態。這個條件是你的'else',然後你只返回0.你可能想要傳遞當前的總和到方法中。 –

+0

@ChrisWohlert沒有必要將當前總和傳遞給方法。檢查我的解決方案 – Zinov

回答

0

你在一行中有解決方案,你應該有一個案例來阻止遞歸,在你的情況下,你停止在49的遞歸,因爲它可以被7整除,並且你不需要int Ø帳號小於49

main() 
{ 
    specialSum(50, 7); 
} 

public static int specialSum(int a, int toDivide) 
{ 
    return (a == 0) ? 0 : (a >= 1 && a%2 == 0 && a%7 != 0) ? a + specialSum(a - 1, toDivide) : specialSum(a - 1, toDivide); 
} 
1

而不是if ((a >= 1) && ((specialSum(a-1))%7 !=0))嘗試if ((a >= 1) && (a%7) !=0)),因爲它是現在,你從來沒有檢查,如果原來的a值不是整除7 ,您的第一個支票總是a - 1.

+0

這不起作用,當我嘗試它。 –

0

您的代碼是錯誤的。

條件(specialSum(a-1))%7 !=0)在您的代碼中調用的方法爲49,當時爲a=50。這將調用48的方法,該方法要求47依次類推,直到a=1。然後,它要求0,這是不大於或等於1,因此它返回0。現在,0%n爲任何數字,是0。因此,你得到0作爲輸出。

你的方法更改爲這樣的事情:

public static int specialSum(int a) { 
    if(a<=2) return 2; // base case 
    else if(a%7==0) return specialSum(a-2); // if it is divisible by 7, then do not add it, and call the method for a-2. 
    else return a+specialSum(a-2); // else add the number, and get the answer for a-2. 
} 
+0

調用值<= 0的函數怎麼樣? (在你的例子中會返回2)。此外,不知道「a%2 == 7」應該檢查什麼......應該不是「a%7 == 0」嗎? –

0

這對我的作品。第一個if確保只需要偶數。然後第二個if確保只有在不能被7整除時才加和。最後的if總和結果。

public static void main(String[] args) { 
    System.out.println(specialSum(50, 0)); 
} 

public static int specialSum(int max, int current){ 
    if(max % 2 == 1) 
     max -= 1; 
    if(max % 7 != 0) 
     current += max; 
    if(max >= 1){ 
     max -= 2; 
     return specialSum(max, current); 
    } 
    return current; 
} 

這將返回566 等於:50 + 48 + 46 + 44 + 40 + 38 + 36 + 34 + 32 + 30 + 26 + 24 + 22 + 20 + 18 + 16 + 12 + 10 + 8 + 6 + 4 + 2

0
public static int specialSum(int a) { 

     if (a % 7 !=0 && a%2==0) { 
     return a + specialSum(a - 2); 
    } else { 
     if (a > 2 ) { 
      a=a-2; 
      return a + specialSum(a - 2); 
     } else { 
      return 0; 
     } 
    } 
} 
+0

這引發一個StackOverflowException。你不能從它回來。 –

1

在遞歸,你只需要關注當前步驟中,不應該用戶specialSum(-1)中的一個條件。這是下一步,您應該只關注當前步驟後再調用它。

你應該只適用於你的兩個規則成功:當前號碼添加到的nextS只有 - 如果他們甚至 - 如果他們不整除7

public static int specialSum(int a) { 
    if(a <= 1) // Final Case. 
    { 
     System.out.print(" 0 = "); 
     return 0; 
    } 


    if(a%2 != 0) // Even Number, do not sum, call next step 
    { 
     return specialSum(a-1); 
    } 
    else 
    { 
     if(a % 7 == 0){ // Divisible by 7 Do not sum, call next step. 
      return specialSum(a-1); 
     } 
     else // NOT divisible by 7 nor by 2, add it to the next step 
     { 
      System.out.print(a+ " + "); 
      return a + specialSum(a-1); 
     } 

    } 

} 

輸出: 50 + 48 + 46 + 44 + 40 + 38 + 36 + 34 + 32 + 30 + 26 + 24 + 22 + 20 + 18 + 16 + 12 + 10 + 8 + 6 + 4 + 2 + 0 = 566

0

你需要檢查,如果是7整除,所以你應該使用if ((a>=1) && (a%7) !=0))以確保該您檢查的基礎條件。