递归方法从整数中移除所有奇数数字。

huangapple go评论76阅读模式
英文:

Recursive method to remove all odd digits from an int

问题

以下是翻译好的内容:

我遇到了编写递归方法的问题,该方法旨在从整数中删除所有奇数位 - 例如:evenDigits(123456) 应返回 246。我最初的想法是将整数转换为字符串,然后通过这种方式进行循环,但问题明确指出不能使用字符串来解决它。

任何帮助都将不胜感激。

编辑:这是我目前的代码,我不确定是否走对了思路,但我无法弄清楚如果最后一位是偶数应该怎么办。

public static int evenDigits(int n) {
    if (n == 0) {
        return 0;
    }
    
    int lastDigit = n % 10;
    
    if (lastDigit % 2 == 0) {
        return evenDigits(n / 10) * 10 + lastDigit;
    } else {
        return evenDigits(n / 10);
    }
}
英文:

I'm having trouble writing a recursive method that is meant to remove any odd digits from an int - for example: evenDigits(123456) should return 246. My first thought was to turn the int into a string and cycle through that way but the question explicitly states you cannot use Strings to solve it.

Any help is appreciated

EDIT: This is what I have so far, I'm not sure if its along the right lines, but I cannot figure out what to do if the last digit was to be even

public static int evenDigits(int n)

if(n==2)
{
	return 2;
}

if(n==1)
{
	return 0;
}

if((n%10)%2==1) //if the last digit is odd
{
	return evenDigits(n/10); //run it again without the last digit
}

答案1

得分: 3

因为这显然是作业,我不能提供完整的解决方案,但以下是一些建议:

当你定义递归时,你应该考虑何时停止(停止条件)。
如果你遵循 @marksplace 提供的提示,那么当递归达到没有数字剩余的点时,你可能会停止。

另一件你需要考虑的事情是在哪里存储“到目前为止累积的结果”。你将会在递归中传递它。在文献中,它甚至被称为“累加器”。
因此,当你达到停止条件时,你将会返回累加器。此外,考虑一下你将如何更新累加器。

这里是一个不涉及代码的工作示例:

  1. 123456
    a. 最后一位是 6,它是偶数,保留它,更新一个累加器(=6)
    b. 对 12345 进行递归调用
  2. 12345 - 最后一位是奇数 - 移除它,
    b. 对 1234 进行递归调用
  3. 1234 - 最后一位是 4 - 它是偶数,保留它,更新累加器 6 -> 46(在这里你需要考虑这种更新的数学公式)

......

最后 - 当你到达没有数字的点时停止,累加器将包含答案

祝你好运!

英文:

Since its obviously the homework, I can't provide a full solution, however here are some hints:

When you define recursion you should think when you stop (a stop condition).
If you follow the hints provided by @marksplace - then you will probably stop when the recursion will reach the point where no digits have left.

Another thing you should think of is where to store the result "accumulated so far". You'll pass it through the recursion. Its even called "accumulator" in the literature.
So when you reach the stop condition you'll return the accumulator. In addition think about how exactly you are going to update the accumulator.

Here is an example of how it can work without diving into the code:

  1. 123456
    a. Last digit is 6, its even, preserve it, update an accumulator (=6)
    b. recursive call for 12345
  2. 12345 - last digit is odd - remove it,
    b. recursivce call for 1234
  3. 1234 - last digit is 4 - its even, preserve it, update the accumulator 6 -> 46 (here you should think about the math formula of such an update)

....

At last - stop when you reach the point, where there are no digits, accumulator will contain the answer

Good luck!

huangapple
  • 本文由 发表于 2020年3月15日 23:09:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/60694315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定