2016-04-25 57 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))以确保该您检查的基础条件。