如何将这个迭代语句转换为递归语句?

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

How can I convert this iterative statement into a recursive statement?

问题

所以我有一个编程项目,需要创建一个程序来确定一个数字是否是一个完美平方数,如果是,就将其写入一个 .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 来实现。任何帮助将不胜感激。

英文:

So I have this programming project in which I need to create a program that determines if a number is a perfect square, and if so, write it into a .txt document. This is very easy and effective to do with a for loop, however, the instructions for the assignment say that the program should accomplish this using recursion. This is the iterative statement I came up with:

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

Where inputs is an ArrayList that is created by reading the inputs of the user.
This solves the problem, but like I said, it needs to be a recursive statement. I know that for recursion I need a base case that will eventually make the method stop calling itself, but I can't figure out what the base case would be. Also, I've seen several examples of converting from iteration to recursion, but all of these examples use a single int variable, and in my case I need to do it with an ArrayList.
Any help would be greatly appreciated

答案1

得分: 1

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

int checkPerfectSquare(long N,
                              long start,
                              long last)
{
    // 找到起始和结束位置的中间值
    long mid = (start + last) / 2;

    if (start > last)
    {
        return -1;
    }

    // 检查是否找到了平方根为 N 的完全平方数
    if (mid * mid == N)
    {
        return (int)mid;
    }

    // 如果平方(mid)大于 N,则表示只有小于 mid 的值可能是 N 的平方根
    else if (mid * mid > N)
    {
        return checkPerfectSquare(N, start,
                                  mid - 1);
    }

    // 如果平方(mid)小于 N,则表示只有大于 mid 的值可能是 N 的平方根
    else
    {
        return checkPerfectSquare(N, mid + 1,
                                  last);
    }
}
英文:

For recursive function, you can use bynary search algorithm:

 int checkPerfectSquare(long N,  
                              long start, 
                              long last) 
{ 
    // Find the mid value 
    // from start and last 
    long mid = (start + last) / 2; 
  
    if (start &gt; 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 &gt; 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); 
    } 
} 

答案2

得分: 1

你可以利用一个完全平方数是奇数整数的和这一事实。例如:

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
英文:

You could use the fact that a square number is the sum of the odd integers. E.g.

1+3 = 4 = 2^2

1+3+5 = 9 = 3^2

1+3+5+7 = 16 = 4^2, etc

public static void main(String[] args) {
    for (int i = 1; i &lt; 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 &lt; sum) return false;
    odd += 2;
    sum += odd;
    return isSquare(n, sum, odd);
  }

output:

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

得分: 0

你可以递归地检查任何较小整数的平方是否等于你的输入。

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);
}
英文:

You could recursively check if the square of any smaller int is equal to your input.

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 &gt; n) return false;
  return isSquare(n,i+1);
}

huangapple
  • 本文由 发表于 2020年9月6日 09:37:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63759967.html
匿名

发表评论

匿名网友

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

确定