微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

这个递归算法的名称?

如何解决这个递归算法的名称?

作业 - 编写两个 Java 程序!

一个使用递归算法。

第二个使用非递归算法。

他们必须确定(任何长度的)列表是否具有以下模式:

单元格[0] = 2;

Cell[1] = 2squared = 4;

Cell[3] = 4squared = 16;

模式是单元格 [n+1] 的任何值等于单元格 [n] 中值的平方。

例如:2、4、16、256、65536、4294967296

问题:

谁能指出我的代码示例?

提前致谢!

解决方法

我所知道的这个问题没有具体的算法,但这里有代码示例:

递归:

public boolean validSequenceFromIndex(int[] sequence,int index) {
  if (index >= sequence.length - 1) return true; // If it is the last index or
  // greater,then it works.

  if (sequence[index + 1] != sequence[index] * sequence[index]) return false; // The
  // pattern does not hold.

  return validSequenceFromIndex(sequence,index + 1); // The sequence is valid at this
  // index,check the rest of the sequence.
}

注意这里的参数是一个 int[] sequence 和一个 int index,而问题应该只给你一个 int[] sequence。只需编写如下函数:

public boolean validSequence(int[] sequence) {
  return validSequenceFromIndex(sequence,0); // Checks if the sequence is valid starting
  // from the beginning (essentially the whole sequence.
}

应该只将序列作为参数转移到使用序列和索引。

非递归:

public boolean validSequence(int[] sequence) {
  for (int i = 0; i < sequence.length - 1; i++) { // Loop through entirety of the
  // except for the last index.
    if (sequence[i + 1] != sequence[i] * sequence[i]) return false;
  }
  // All indices checked,the sequence works:
  return true;
}

希望这对您有意义!

,

这是一种使用 BigInteger 的方法。但即便如此,我还是将术语的数量限制为 8,因为它们变得非常大。

迭代调用。

BigInteger[] terms = iterative(8);
for (BigInteger b : terms) {
    System.out.println(b);
}
System.out.println("Sequence array for iteration is " + 
(validate(terms) ? "valid" : "invalid"));

印刷品

2
4
16
256
65536
4294967296
18446744073709551616
340282366920938463463374607431768211456
Sequence array for iterative is valid

递归调用

terms = recursive(8);
for (BigInteger b : terms) {
    System.out.println(b);
}
System.out.println("Sequence array for recursion is " + 
(validate(terms) ? "valid" : "invalid"));

印刷品

2
4
16
256
65536
4294967296
18446744073709551616
340282366920938463463374607431768211456
Sequence array for recursion is valid

验证方法

public static boolean validate(BigInteger[] terms) {
    for (int i = 1; i < terms.length; i++) {
        if (!terms[i].equals(terms[i-1].pow(2))) {
            return false;
        }
    }
    return true;
}

迭代方法。

  • 只需将第一项初始化为 Biginteger.TWO
  • 然后对列表进行迭代,将之前的每一项都提高到 2 的幂。
public static BigInteger[] iterative(int n) {
   if (n < 1) {
        throw new IllegalArgumentException("n must be > 0");
    }
    BigInteger[] terms = new BigInteger[n];
    terms[0] = BigInteger.TWO; // 2^2^0 = 2;
    for (int i = 1; i < n; i++) {
        terms[i] = terms[i-1].pow(2);
    }
    return terms;
}

递归方法。

虽然它可以在没有辅助方法的情况下完成,但使用一个更直接和有效。

  • 根据n分配数组
  • 0th 元素初始化为 2
  • 如果 n == 1
  • 立即返回
  • 否则,调用辅助方法。
public static BigInteger[] recursive(int n) {
    if (n < 1) {
         throw new IllegalArgumentException("n must be > 0");
    }
    BigInteger[] terms = new BigInteger[n];
    terms[0] = BigInteger.TWO;
    if (n == 1) {
        return terms;
    }
    return recursiveHelper(terms,n);
}
  • 递归调用该方法直到n == 2
  • 然后只需将 n-1 中的值指定为 n-2 元素的 2 次幂
  • 然后返回条款。
private static BigInteger[] recursiveHelper(BigInteger[] terms,int n) {
     if (n > 2) {
        recursiveHelper(terms,n-1);
     }
     terms[n-1] = terms[n-2].pow(2);
     return terms;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。