【问题标题】:How can I convert this iterative statement into a recursive statement?如何将此迭代语句转换为递归语句?
【发布时间】:2020-12-24 20:04:00
【问题描述】:

所以我有这个编程项目,我需要在其中创建一个程序来确定一个数字是否是一个完美的正方形,如果是,将它写入一个 .txt 文档。使用 for 循环执行此操作非常容易且有效,但是,赋值指令表明程序应使用递归来完成此操作。这是我想出的迭代语句:

double division;
        for (int i = 0; i < inputs.size(); i++) {
            division = (Math.sqrt(inputs.get(i)));
            if (division == (int)division) {
                pw.println(inputs.get(i));
                 }
            }

其中inputs 是通过读取用户输入创建的ArrayList。 这解决了问题,但就像我说的,它需要是一个递归语句。我知道,对于递归,我需要一个最终使方法停止调用自身的基本案例,但我无法弄清楚基本案例是什么。此外,我还看到了几个从迭代转换为递归的示例,但所有这些示例都使用单个 int 变量,在我的情况下,我需要使用 ArrayList 来完成。 任何帮助将不胜感激

【问题讨论】:

  • 您的函数与 inputs() 是一个数组列表这一事实无关......它只使用 inputs.get(i) ,它只是一个整数变量。
  • 是的,我没有这样想

标签: java recursion arraylist iteration


【解决方案1】:

您可以递归检查任何较小 int 的平方是否等于您的输入。

public static boolean isSquare(int n) {
  if (n==0 || n==1) return true;
  return isSquare(n, 1);
}

private static boolean isSquare(int n, int i) {
  if (i*i == n) return true;
  if (i*i > n) return false;
  return isSquare(n,i+1);
}

【讨论】:

    【解决方案2】:

    您可以使用平方数是奇数之和这一事实。例如

    1+3 = 4 = 2^2

    1+3+5 = 9 = 3^2

    1+3+5+7 = 16 = 4^2,以此类推

    public static void main(String[] args) {
        for (int i = 1; i < 1000; i++) {
          if (isSquare(i)) System.out.println(i);     
        }
      }
      public static boolean isSquare(int n) {
        if (n==0 || n==1) return true;
        return isSquare(n,1,1);
      }
    
      private static boolean isSquare(int n, int sum, int odd) {
        if (n==sum) return true;
        if (n < sum) return false;
        odd += 2;
        sum += odd;
        return isSquare(n, sum, odd);
      }
    

    输出:

    1
    4
    9
    16
    25
    36
    49
    64
    81
    100
    121
    144
    169
    196
    225
    256
    289
    324
    361
    400
    441
    484
    529
    576
    625
    676
    729
    784
    841
    900
    961
    

    【讨论】:

      【解决方案3】:

      对于递归函数,可以使用二叉搜索算法:

       int checkPerfectSquare(long N,  
                                    long start, 
                                    long last) 
      { 
          // Find the mid value 
          // from start and last 
          long mid = (start + last) / 2; 
        
          if (start > last) 
          { 
              return -1; 
          } 
        
          // Check if we got the number which 
          // is square root of the perfect 
          // square number N 
          if (mid * mid == N) 
          { 
              return (int)mid; 
          } 
        
          // If the square(mid) is greater than N 
          // it means only lower values then mid 
          // will be possibly the square root of N 
          else if (mid * mid > N) 
          { 
              return checkPerfectSquare(N, start,  
                                        mid - 1); 
          } 
        
          // If the square(mid) is less than N 
          // it means only higher values then mid 
          // will be possibly the square root of N 
          else 
          { 
              return checkPerfectSquare(N, mid + 1,  
                                        last); 
          } 
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-16
        • 2019-07-03
        • 1970-01-01
        • 1970-01-01
        • 2019-07-04
        • 1970-01-01
        • 2021-03-07
        • 1970-01-01
        相关资源
        最近更新 更多