【问题标题】:Duodecimal Recursive Convert an Integer to a String十进制递归将整数转换为字符串
【发布时间】:2019-10-12 22:54:23
【问题描述】:

我不知道如何为 String 值分配一个数字并使其自动识别以使该数字成为 String 值。问题如下:

递归编写方法: 1) 考虑在小于 12 时仅返回数字本身的基本情况(除以 12 时商为 0,余数为数字)。如果数字是 10 或 11,则返回“A”或“B”。

2) 考虑数字大于或等于 12 的递归情况。在这种情况下,首先使用商递归调用方法并将结果(使用 + 运算符连接字符串)与另一个结果连接使用余数递归调用。返回两个递归调用的连接结果。

一个例子; 20A 将是:

2 x 12^2 + 0 x 12^1 + 10 x 12^0 = 2 x 144 + 0 x 12 + 10 x 1 = 288 + 0 + 10 = 298

我现在知道代码完全错误

public class Duodecimal {
  public static String toBase12(int n) {
    //TODO: complete this method
    if (n==10)
    return "A";
    if (n==11)
    return "B";
    if (n<12)
    return n;
    else {
      if (n%12==10)
      return (n/12 + "A");
      if (n%12==11)
      return (n/12 + "B");
      else
      return n/12;
    }
  }
}

【问题讨论】:

  • "return "A" or "B" if the number is 10 or 11" 这意味着A=10和B=11,那么为什么20A使用1 x 12^0,什么时候应该是10 x 12^0
  • 次要但 20A 将是 2x12^2 +0x12^1 + 10x12^0 = 2x144 + 0x12 + 10x1 = 288 + 0 + 10 = 298
  • 我建议尝试这样的方法 - 获取输入 N 并计算 N/12 和 N%12。然后添加 N%12 的字符串值并在 N/12 的值上递归(尾递归)。
  • "考虑大于等于12的数的递归情况。这种情况下先递归调用方法使用商"我在else 块中没有看到这样做的尝试。
  • 错字修正谢谢!

标签: java recursion


【解决方案1】:

您几乎正确地完成了 #1(return n 无法编译),所以让我们看看 #2:

考虑大于或等于 12 的数字的递归情况。在这种情况下,首先使用商递归调用该方法,并将结果(使用 + 运算符连接字符串)与另一个递归调用的结果连接起来使用剩余部分。返回两个递归调用的连接结果。

让我们一步一步来,处理粗体部分。

首先递归调用方法使用商

int quotient = n / 12;

首先递归调用方法使用商

toBase12(quotient)

另一个递归调用的结果使用余数

int remainder = n % 12

另一个递归调用的结果使用余数

toBase12(remainder)

[...] 和 concatenate 将结果(使用 + 运算符连接字符串)与 [...]

toBase12(quotient) + toBase12(remainder)

返回两个递归调用的拼接结果

让我们也消除变量:

return toBase12(n / 12) + toBase12(n % 12)

如您所见,作业包括您应该做什么的分步说明。您所要做的就是编写向您解释的代码。

最终结果是:

public static String toBase12(int n) {
    if (n < 10)
        return Integer.toString(n); // or  String.valueOf(n)  or  "" + n
    if (n == 10)
        return "A";
    if (n == 11)
        return "B";
    return toBase12(n / 12) + toBase12(n % 12);
}

测试

System.out.println(toBase12(298));

输出

20A

【讨论】:

  • 谢谢!很好的解释。非常感谢!
【解决方案2】:

在一张纸上,你会这样:

  1. 将输入除以十二并保存余数。
  2. 将结果再次除以十二并保存余数。
  3. 继续执行第二步,直到结果为零。连接 从下到上每一步的余数。

使用您的示例输入:

298 / 12 = 24 rem 10 (A)
 24 / 12 = 2  rem 0
  2 / 12 = 0  rem 2

因此

298 in base 10 = 20A in base 12

【讨论】:

    【解决方案3】:

    这是一种使用尾递归的方法。第二种方法会累积,直到你降到零然后返回。

    public class Base12 {
        public static String toBase12(int n) {
            return toBase12(n, "");
        }
    
        private static String toBase12(int n, String value) {
            return n <= 0 ? value : toBase12(n/12, toBase12Char(n%12) + value);
        }
    
        private static char toBase12Char(int n) {
            return n == 11 ? 'B' : (n == 10 ? 'A' : Integer.toString(n).charAt(0));
        }
    }
    
    public class Base12Test {
        @Test
        public void Test20A() {
            int n = 298;
    
            String expectedValue = "20A";
            String actualValue = Base12.toBase12(n);
    
            Assert.assertEquals(expectedValue, actualValue);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 1970-01-01
      • 2015-11-29
      • 2015-06-14
      • 2016-10-04
      • 2014-12-15
      相关资源
      最近更新 更多